kellzer
kellzer

Reputation: 131

Why is my server response printing incorrectly?

I am studying for an exam and am working on the following sample problem. I have a simple server that I am trying to connect to with a client. The connection is working fine it seems but for some reason this is the output that I am getting in the client console. Can anyone tell me why this is happening?

NOTE: I am able to access the Message class from client side as well (which I have not done because I'm not sure how that will help but I think that this is the key to solving this problem) if anyone can make a suggestion of what to do.

Also, any changes that I make have to be to the code on the client side. The server side code cannot change. I have attached my code below as well. Thanks

Console Screenshot

public class Client {
public static void main(String[] args) throws IOException {
    Socket s = new Socket("localhost", 8999); // create a new socket
    InputStream instream = s.getInputStream(); // Create input Stream obkect
    OutputStream outstream = s.getOutputStream(); // Create output stream
                                                    // object
    Scanner in = new Scanner(instream); // Create scanner object that takes
                                        // the instream as an argument
    PrintWriter out = new PrintWriter(outstream); // Create a printwriter
                                                    // outstream object

    String request="GET / HTTP/1.0\n\n";
    out.print(request); // pass the request to the outstream
    out.flush(); // moves all the data to the destination
    String response = in.nextLine(); // response is the next line in the
                                        // input stream
    System.out.println("Receiving: " + response); // Print statement

    s.close(); // close the socket
}
}


public class TimeServer {

private static Date currentDateAndTime() {
    return new Date();
    // (An object of class Date comprises time and date)
}

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(8999);
        while (true) {
            Socket socket = serverSocket.accept();
            try {
                ObjectOutputStream stream = new ObjectOutputStream(
                        socket.getOutputStream());
                Date dt = currentDateAndTime();
                Message m = new Message(dt);
                stream.writeObject(m);
                stream.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            try {
                socket.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
}


class Message implements Serializable { 
//This helper class can be used both by server and client code 

Date timeAndDate; 
// (Objects of class Date comprise time & date) 

Message(Date timeAndDate) { 
this.timeAndDate = timeAndDate; 
} 

}

Upvotes: 0

Views: 95

Answers (1)

babanin
babanin

Reputation: 3584

You need to modify only client side:

Scanner in = new Scanner(instream); // Create scanner object that takes
                                    // the instream as an argument

replace with

ObjectInputStream in = new ObjectInputStream(instream); 

Also fix reading from input stream, replace:

String response = in.nextLine();
System.out.println("Receiving: " + response);

with:

Message response = (Message) in.readObject();
System.out.println("Receiving: " + response.timeAndDate);

Upvotes: 1

Related Questions