Skip to content
Advertisement

Java Server Socket Socket program stuck while reading

i have 2 programm one is a server and the other the client , i have some other class who represent geomtric shape. In the client with the scanner the user write the caracteristics of the shape width,length for a rectangle as an example. the he create an object and send it to the server , the server receive the object calculate area and perimeter and send them to the client. This should happen until the user press enter in the client instead of caracteristics. And i don’t know why but the first loop work good but the second one i type my caracteristics and then nothing happen anymore and my programms are stuck . Maybe you can see my error here are the 2 programm :

Ps: i’m French so sorry for the french word use for my variable’s name Server:

JavaScript

And my client :

JavaScript

Advertisement

Answer

Your server socket does the following:

  • Wait for a connection to happen, and accept it. (this is sockConn.accept())
  • Receive a request from that connection
  • Send a response to that connection
  • Wait for a connection to happen, and accept it.
  • Receive a request from that connection
  • Send a response to that connection

i.e. your server only expects one request per connection.

Your client does this:

  • Connect to the server
  • Send a request
  • Receive a response
  • Send a request
  • Receive a response

When your client is waiting for the 2nd response, your server is waiting for the 2nd connection, so nothing happens.

You can fix it by doing one of these things:

  • Make the client start a new connection for every request
  • Make the server handle many requests on the same connection (instead of waiting for a new one)
  • Make the server start a new thread for every connection, so it can process several connections at the same time
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement