• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

ServerSocket not sending the responce to client socket that is connected

 
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
All of the following truths are shameless lies. But what about this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic