Problem writing network code in Java...

jman19

Lifer
Nov 3, 2000
11,222
654
126
Hey all, I'm a newb with regards to writing code to do any network programming, and I've run into a problem along the way. Basically, I start a program in "network mode" (it runs as a service) on a linux server, and try to get my Java code to receive messages from the server. After receiving the text from the server, I want to then send commands to the program. I set up a while loop to read lines from the input stream, and after receiving everything, to start writing out to the server. However, I never get out of the while loop that reads from the input stream. The attached code simply tries to read from BufferedReader until there is nothing left to read... I never leave that loop, but it doesn't loop forever; everything just seems to "hang." Any help would be appreciated! Thanks.
 

itachi

Senior member
Aug 17, 2004
390
0
0
the program you wrote is the client.. it sends commands to the server and the server responds, not the other way around. you have to send a request to the server before you can wait for a response, otherwise you got both the server and the client waitin for a response from one another..
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Itachi, the program on the server waits for a connection on the port specified (used as a command line arg). When I connect the socket to that port on the server, the server side program then responds with some text, asking the user to enter some commands. What I'm curious about is why after I receive this text from the server that I can't get out of that loop that reads from the BufferReader object. Note that I do infact get a response, it's just that the code doesn't get past the loop, and the loop isn't infinite. For now, I was just trying to get a response and then send the appropriate commands later (in this case, I have to send it a string). Thanks.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Trying to read on a socket that has no more data blocks until more comes (otherwise you'd have to poll it, which is a bad thing). Usually your protocol will dictate when you stop reading because otherwise network delays can cause you to stop reading too early. If you're sure that you want to detect when no more data is coming, try using BufferedReader.isReady() as the condition for your loop.
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Originally posted by: kamper
Trying to read on a socket that has no more data blocks until more comes (otherwise you'd have to poll it, which is a bad thing). Usually your protocol will dictate when you stop reading because otherwise network delays can cause you to stop reading too early. If you're sure that you want to detect when no more data is coming, try using BufferedReader.isReady() as the condition for your loop.

OK kamper, I'll give that a try. Thanks.
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Kamper, the BufferedReader ready() function causes the loop to terminate before all of the data is actually sent... the loop terminates before entering the body, whereas when just reading lines from the BufferedReader at least allows me to enter the loop body and retrieve data sent from the server... Still confused as to why I can't get my code to work
 

znaps

Senior member
Jan 15, 2004
414
0
0
You say it never leaves the while loop, so the server is not closing the connection. Are you sure your client should be set up to expect NULL as the sign to switchover to sending data?
 

daMachine

Senior member
Oct 30, 2001
322
0
76
Does it just "hang" in the loop?

I haven't done much java newtwork programming in a while, but I think if you are waiting for the loop to break via "(responseLine = is.readLine()) != null" it will never happen unless your server explicitly sends NULL to the client (or use a designated end of transmission ACK to signify when the server is done sending).
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You're going to have to either plan when to stop listening, based on the protocol, or set a timeout on the socket (should set a timeout anyways). Using isReady() wasn't really a good idea anyways.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |