1

Update - Moving to consistent type provided solution

Client sends message to server socket, the server then responds to the client with original message. When introducing the latter functionality, the server only receives one message rather than continuing to receive said messages, and does not respond to client. Commented on lines that were added. Any insight into hangup would be great.

Client side, issues commented and code update from responses:

{ private Socket socket = null;
 private BufferedReader console = null;
 private DataInputStream streamIn = null;
 private DataOutputStream streamOut = null;

 while (!line.equals(".bye"))
 { try
 { line = console.readLine();
 streamOut.writeUTF(line); //Send console data to server socket
 String reply = streamIn.readUTF(); //Recieve confirmation msg from server
 System.out.println( reply ); //Print the msg
 streamOut.flush();
 }

 public void start() throws IOException
 { console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
 streamIn = new DataInputStream(socket.getInputStream());
 streamOut = new DataOutputStream(socket.getOutputStream());
 }
 public void stop()
 { try
 { if (console != null) console.close();
 if (streamOut != null) streamOut.close();
 if (streamIn != null) streamIn.close(); //Is it good practice to close
 if (socket != null) socket.close();
 }

Server side, issues commented.

 public void handleClient() throws IOException {
 boolean done = false;
 try {
 System.out.println("Server Thread " + ID + " running.");
 while (!done) {
 String nextCommand = streamIn.readUTF();
 if( nextCommand.equals(".bye") ) {
 System.out.println("Client disconnected with bye.");
 done = true;
 } else {
 System.out.println( nextCommand );
 String nextReply = "\"You sent me ->" +nextCommand.toUpperCase() + "\"\n";
 streamOut.writeUTF( nextReply );
 }
 }
 } finally {
 streamIn.close();
 streamOut.close();
 socket.close();
 }
 }
 public void open() throws IOException
 {
 streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
 streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
// streamOut = new DataOutputStream(socket.getOutputStream());
 }
 public void close() throws IOException
 { if (socket != null) socket.close();
 if (streamIn != null) streamIn.close();
 if (streamOut != null) streamOut.close();
 }
asked Feb 29, 2012 at 5:46
1
  • Does this compile? Shouldnt it be streamOut.writeBytes( nextReply.getBytes() ); ? Commented Feb 29, 2012 at 8:31

1 Answer 1

1

You are using writeUTF and readUTF but in one place streamIn.readLine() which I would expect to block as it waits for a new line. I suspect you need to use readUTF consistenly.

BTW the console is not a data stream, its text and I suggest you use BufferedReader.

answered Feb 29, 2012 at 8:20
Sign up to request clarification or add additional context in comments.

8 Comments

I have move from UTF to readLine() and writeBytes() and hopefully buffered appropriately. Should I have just stayed with *UTF() as the server is no longer receiving data from the client?
As I pointed out readLine() waits for a newline so I assume you are sending a new line at the end of your writeBytes(). Otherwise, I would just use writeUTF/readUTF as I have suggested.
Reverted to read/writeUTF, similar symptoms as before, client is able to send over initial msg, after that, no more msgs are received.
You are calling flush() after each writeUTF() and your client is blocking on readUTF() (you can see this in your debugger)
Moved writeUTF(), is this also blocking readUTF()? How should I invoke the debugger to better understand what's happening?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.