• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServerSocket not sending the responce to client socket that is connected

 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am just refreshing my Java skills, I got stuck when I was trying to implement a Server Socket and Socket as described in Head First Java. The problem I am facing here is when I send a message from client socket to server it reaches there but when server has to send the same message back to client it fails. But when I stop the server the ClientHandler thread executes and send the message to client socket and I am able to see it on the UI and console. This program is provided as an example in Head First Java, I thought it worked but looks like the server thread is getting stuck, thanks for your help in advance.

Here is the code for client and server and the console output
ChatServer.java


SimpleChatClient.java


Output:

Server Console
suman@suman-pc:~/workspace/ChatServer/bin$ java -classpath . com.chatserver.server.ChatServer
Got a connection
Server-read Hello
^Csuman@suman-pc:~/workspace/ChatServer/bin$ <---------Server program terminated here

Client Console
suman@suman-pc:~/workspace/ChatClient/bin$ java -classpath . com.chatclient.ui.SimpleChatClient
Network Established!
client-read Hello from Server!!!Hello <------------- This was sent after the Server program was terminated and client is still running. Ideally the socket termination error should show up.



 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind guys I got it working. I replaced the with in the server class. After this code change I was able to see the response on the client console.

Look like there is some difference between print() and println() methods. If any info please share
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suman Mummaneni wrote:
Look like there is some difference between print() and println() methods. If any info please share


Yes there is:
public void println()
Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').


PrintWriter.println()

In your client code, you are using BufferedReader.readLine:
public String readLine()
throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.


If you don't send an end-of-line character, readLine waits for one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!