Reputation: 39
Server:
Socket client_socket = server_back_end.server_socket.accept(); //1.
ClientInfo new_client = new ClientInfo(
new ObjectOutputStream(client_socket.getOutputStream()), //writer
new ObjectInputStream(client_socket.getInputStream()), //reader
"blah, about to be re-written"
);
new_client.user_name = (String) new_client.reader.readObject(); //3. "bob"
//new_client.user_name = new_client.reader.readLine(); //this doesnt work either
System.out.println("Client " + new_client.user_name + " has connected.");
Client:
Socket client_socket = new Socket();
client_socket.connect(new InetSocketAddress(server_ip, server_port), 500);
client_front_end.display("<<Connection Successful.>>\n");
String user_name = txtfield.getText(); //"bob"
client = new ClientInfo(
new ObjectOutputStream(client_socket.getOutputStream()), //writer
new ObjectInputStream(client_socket.getInputStream()), //reader
user_name);
client.writer.writeChars(client.user_name); //2.
I first run the server code. When I step through the debugger on the server side, "//1." executes, and waits for a client to connect.
Then I run the client code. What I expect to happen is "//2." should call "//3." in the server code, pick up the name, and print that the user has connected. But the
ObjectOutputStream.writeChars(client.user_name)
is not triggering the read in the server. What am I doing wrong? My goal is to successfully print the name from the client using these ObjectOutput/ObjectInput streams
Upvotes: 0
Views: 49
Reputation: 311052
You're reading an object but you aren't writing an object. You're writing chars. readObject()
can only read the output of writeObject()
. readInt()
can only read the output of writeInt()
. And so on.
Upvotes: 1