views:

3376

answers:

6

Hello, I am really thankful for everyone who would read this and try to help me, the following is the code I am trying to write for a server class for a socket-programming project for college:

import java.io.*;
import java.net.*;
import java.io.File;

class Server{
    public static void main (String[]args)throws IOException{
     ServerSocket socket1 = new ServerSocket (8000);
        while (true) {
            Socket incoming = socket1.accept();
            new newclass(incoming).start();
        }
    }
}

class newclass extends Thread implements Runnable {

    Socket incoming;

    public newclass(Socket incoming) {
        this.incoming = incoming;
    }

    public void run() {
        try {
            byte x = 0;
            String z;
            String s = "HTTP 1.0 200 Document follows";
            String s1 = "Bad request message";
            BufferedReader input = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter output = new PrintWriter(incoming.getOutputStream(), true);
            DataOutputStream sending = new DataOutputStream(incoming.getOutputStream());
            File directory = new File("C:\\Documents and Settings\\Ahmed\\Desktop\\bla\\Server");
            File[] files = directory.listFiles();
            int x1 = files.length;
            if ((x1 - 3) < 10) {
                boolean done = false;
                while (!done) {
                    String line = input.readLine();
                    System.out.println(line);
                    if (line.equals("BYE")) {
                        output.println("BYE");
                        done = true;
                    } else {
                        if (line.trim().substring(0, 3).equals("GET ")) {
                            if (line.equals("<javalogo.png> HTTP    1.0")) {
                                File f = new File("javalogo.png");
                                int size = (int) f.length();
                                if (f.exists() == true) {
                                    output.println(s);
                                    output.println(size);
                                    output.println("javalogo1.png");
                                    DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
                                    while (bytefile.available() != 0) {
                                        x = bytefile.readByte();
                                        sending.write(x);
                                    }
                                } else {
                                    System.out.println("Getting file from main server");
                                    Socket socket2 = new Socket("127.0.0.1", 8100);
                                    BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
                                    PrintWriter pOut = new PrintWriter(socket2.getOutputStream(), true);
                                    BufferedReader bIn = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
                                    pOut.println("GET <javalogo.png> HTTP 1.0");
                                    String rep = bIn.readLine();
                                    if (rep.equals("HTTP 1.0 200 Document follows")) {
                                        int len = Integer.parseInt(bIn.readLine());
                                        String fname = bIn.readLine();
                                        File f1 = new File(fname);
                                        f1.createNewFile();
                                        FileOutputStream fos = new FileOutputStream(f1);
                                        DataInputStream dis = new DataInputStream(socket2.getInputStream());
                                        for (int i = 0; i < len; i++) {
                                            fos.write(dis.read());
                                        }
                                        fos.close();
                                    } else if (rep.equals("File does not exist")) {
                                        output.println("Sorry, but the file was neither found in the proxy server or the main server or the name is wrong.");
                                    }

                                }
                            }
                            File f2 = new File("javalogo.png");
                            if (f2.exists() == true) {
                                int size = (int) f2.length();
                                output.println(s);
                                output.println(size);
                                output.println("javalogo.png");
                                DataInputStream bytefile = new DataInputStream(new BufferedInputStream(new FileInputStream(f2)));
                                while (bytefile.available() != 0) {
                                    x = bytefile.readByte();
                                    sending.write(x);
                                }
                            }
                        } else {
                            System.out.println(s1);
                            output.println(s1);
                        }
                    }
                }
                incoming.close();

            }
            output.println("Connecting to main server");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Now I don't understand why am I getting an error when I run the following client on it.
I get this really weird error where the buffered reader reads the first line from the user correctly but with the second one it gives me a null exception as if the client wrote null or something, I dont get it.

Here's the client code anyways, if anyone can help me I would be plenty thankful.

import java.net.*;
import java.io.*;


public class Client {

    public static void main(String[] args) throws Exception {

        Socket socket1 = new Socket("127.0.0.1", 8000);

        BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
        BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
        String cmd;
        String rep;
        while (true) {
            cmd = bUsr.readLine();
            pOut.println(cmd);

            System.out.println(rep = bIn.readLine());

            if (cmd.equals("BYE") || cmd.equals("END"))
                break;
            else if (rep.equals("HTTP 1.0 200 Document follows")) {
                int len = Integer.parseInt(bIn.readLine());
                String fname = bIn.readLine();
                File f = new File(fname);
                f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                DataInputStream dis = new DataInputStream(socket1.getInputStream());
                for (int i = 0; i < len; i++) {
                    fos.write(dis.read());
                }

                fos.close();
                System.out.println("Success");

            } else if (rep.equals("Connecting to main server")) {
                Socket socket1 = new Socket("127.0.0.1", 8100);
                BufferedReader bUsr = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter pOut = new PrintWriter(socket1.getOutputStream(), true);
                BufferedReader bIn = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
                String cmd;
                String rep;
                while (true) {
                    cmd = bUsr.readLine();
                    pOut.println(cmd);

                    System.out.println(rep = bIn.readLine());

                    if (cmd.equals("BYE") || cmd.equals("END"))
                        break;
                    else if (rep.equals("HTTP 1.0 200 Document follows")) {
                        int len = Integer.parseInt(bIn.readLine());
                        String fname = bIn.readLine();
                        File f = new File(fname);
                        f.createNewFile();
                        FileOutputStream fos = new FileOutputStream(f);
                        DataInputStream dis = new DataInputStream(socket1.getInputStream());
                        for (int i = 0; i < len; i++) {
                            fos.write(dis.read());
                        }

                        fos.close();
                        System.out.println("Success");
                    }
                }
            }

            bIn.close();
            pOut.close();
            socket1.close();
        }
    }

This is the first time asking anything on this site, so if I did anything wrong I would be more than happy to change what I wrote.

By the way, the part in the server which states "getting file from main server" is a part where the server itself becomes a client for a main server, from where it gets the file and sends it to the client, I didn't add the main server code because it'd be too much code but basically it's the same as server without the if condition restricting it to 10 files in the directory only.

A: 

I'd be lying if I said I fully understand what your code is doing - But if you are getting null data when you are reading from a stream which you expect to contain data it could be that the output stream hasn't 'flushed' the data.

Make sure you call the flush() method on your Output streams after you have written to them

Richie_W
A: 

First of all my code generates a socket and connects a client to it.

Then the client sends a GET statement to the server requesting the file javalogo.png from the server:

  • if it exists in the directory then it sends it through a byte file,
  • otherwise it sends the same request message to the main server and acts as a client, receiving javalogo.png from it then sending it to the client.

But the directory of the "proxy" server cant exceed 10 files so if the directory has more than 10 files other than the .class and .java files, it connects the client to the main server directly.

I hope I made the code clear.

A: 

If anyone can help me i really need this a project and the deadline is tommorow, please anyone, Thanks in advance.

A: 

Ahemd, I see a number of potential errors and some actual bugs too in the classes as posted, but the code is too complicated for me to focus on the issue you are seeing. If you're still experiencing network bugs, try reducing the code and both client and server to the minimal possible to get communication going (send/read one line). If that doesn't work for you, post those classes and we'll be able to see the problems much more quickly than with the larger classes posted here.

Good luck.

Dov Wasserman
+1  A: 

In general, when there is a NullPointerException either:

  • You have not instantiated your object
  • Your object has been destroyed (closed) and therefore does not exist
  • You have an invalid cast
  • Your code has overwritten your object pointer

You would need to examine your stack dump to see which of these is true.

From the Jav Docs the read can throw IOException if an I/O error occurs and IOException can give you the specified detail message. The error message string can later be retrieved by the Throwable.getMessage() method of class java.lang.Throwable.

Two points:

  • What does the IOException detail give you?
  • Since this is for your college course, try asking your classmates or TA for assistance
Noah
A: 

If you are getting null from a BufferedReader.readLine() it means you have reached the end of input as it states in the javadoc for this method.

Peter Lawrey