Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A client hangs reading from a Sockets  RSS feed

 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The program hangs at String reply = bufferedReader.readLine();

The server code is

The line System.out.println(messageString); verifies that the server should be sending a reply.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro

The program hangs doesn't help Tell The Details
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. What happens is that on the debugger the program stays at that line for ever. If I run the program I never get a result (I just tried with a print statement in the next line).
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro Barrero wrote:Thank you for your help. What happens is that on the debugger the program stays at that line for ever. If I run the program I never get a result (I just tried with a print statement in the next line).


Putting a print statement on the next line is not guaranteed to run. If the socket read call throws an IOException, it will never get to the next line. And your method is defined to throws it (ie. not handle it).

Henry
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro Barrero wrote:
The line System.out.println(messageString); verifies that the server should be sending a reply.


Do you mean this line actually executed and printed something?

It's kind of strange cause it looks like that for your case the server and the client should both be blocked ( unless I am overlooking something :-) ).


Edit - My bad. I read it wrong. You meant that's the expected behavior. Your client and server are both waiting to read from the input streams. That is why your client and server are both hung.
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out that the problem is that I am not reading from the same socket that send the request.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro,

Alejandro Barrero wrote:I am not reading from the same socket that send the request.


What does this statement mean? Kindly elaborate.
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The the reader is not constructed from the same socket used to create the writer that sent the message.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro Barrero wrote:The the reader is not constructed from the same socket used to create the writer that sent the message.


Assuming that you are talking about the client, the code that you showed doesn't write to the socket, or even create the writer. And since the socket is only referred to by a local variable, if you have some code elsewhere in you program that creates the writer, agreed, it is not using the same socket object.

Henry
 
Alejandro Barrero
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for confusing you. Threads send the requests and another program with a different socket attempts to get the responses.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro Barrero wrote:My apologies for confusing you. Threads send the requests and another program with a different socket attempts to get the responses.


That is how sockets work. They represents the endpoints of a connection. So, applications uses different sockets, and whether they can communicate depends on whether they are for the opposite endpoints of the same connection.

Henry
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff,

Thanks for commenting here. I was totally confused by alejandro's post about same sockets.

Alejandro,

Have you written your client and server in different projects or have you written them in same project? As Sheriff has said, sockets are endpoints of connection. First try writing sockets in different projects and connect them. Then try to send data.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!