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();
}
-
Does this compile? Shouldnt it be streamOut.writeBytes( nextReply.getBytes() ); ?Zak– Zak2012年02月29日 08:31:42 +00:00Commented Feb 29, 2012 at 8:31
1 Answer 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.
8 Comments
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?writeUTF(), is this also blocking readUTF()? How should I invoke the debugger to better understand what's happening?