Solved

problem with I/O

Posted on 2002-06-30
14
215 Views
Last Modified: 2010-03-31
I have a program that reads sends commands to a server, and reads the message back from the server.  The way I have it written now, the code looks like this:

//s is the string to be sent to the server.
//out is a PrintWriter to the sockets OutputStream
//in is a Buffered Reader of the sockets InputStream
if(s != null) {
   System.out.println("C: " + s);
   out.println(s);
   out.flush();
}
String line = in.readLine();
while (line != null) {
   System.out.println("S: " + line);
   line = in.readLine()
}

The way I want the program to work is to send a command to the server, and then read all the lines the server sends back.  The problem is that the program freezes when there are no more lines to be read from the server.

How can I change this so the program knows not to read a line when there is nothing to be read?

Thanks
0
Comment
Question by:lwinkenb
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7120743
The server needs to either close the connection, or send some indication. Otherwise the client has no way of knowing whether the server has more data to send.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7120782
I dont have any control over what the server does, I'm only working with the client.  Also, the server doesn't give any indication that the current line would be the last one.

I was thinking of creating a thread to listen for server responses, but Im pretty new to thread programming, and Im not sure if that would work.
0
 
LVL 92

Expert Comment

by:objects
ID: 7120798
If the server provide no indication then there's not much you can do. Just cause there's nothing available to read doesn't mean the servers finished, there may just be network congestion for example.

> thinking of creating a thread to listen for server responses

Unclear how this will help as this is exactly what your curreent thread is doing.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7120834
In my case I know the server is finished.

Let's say for example I use the above code to send the "EHLO" command to a smtp server.  The server might respond with

250-smtp.server.com Hello [xxx.xx.x.xx]
250-TURN
250-ATRN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK

My program would read in all the lines and display them, and then it would freeze after sending the last line.  Now I could tell my program to stop reading lines after it sees "250 OK", but the server does not respond with "250 OK" as the last line every time it sends something to the client.
0
 
LVL 92

Expert Comment

by:objects
ID: 7120838
> In my case I know the server is finished.

If you know when the server is finished then stop reading.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7120850
There is no magical way to determine if the server has no more data to send. The client can only act according to what data it has recieved and this is typlically defined by the protocol being used.
0
 
LVL 3

Expert Comment

by:rjackman
ID: 7120983
agreed with objects
there has to be some way to determine that server has finished sending the data
in such case u need to change the server response & force it to send 250 OK or some other string to indicate that  it has finished sending data
cheers
RJ
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:mzimmer74
ID: 7121087
The other option, although it's an ugly one, is to know exactly what the format of any possible message you can receive from the server.  Then, as you receive various messages, you should know what the ending point is for any given message.  Then it's simply a matter of parsing the information as you go to determine what end message you are looking for.
0
 
LVL 3

Expert Comment

by:daitt
ID: 7122541
I think it's not an ugly option, it's really what we need to do, understand each response and know where is the end of response.
As in SMTP protocol, here are some notes:
1. 250 OK ends the response and indicate success.
2. DATA response end with a period and then 250 OK.
3. Other codes (not 250) indicate failures and end the response.
You can read more about SMTP protocol at:
http://www.ietf.org/rfc/rfc0821.txt
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 7122662
So all you have to do then is upon reading each line from the server, call some method that checks to see if it's any end response.  Basically, you can do the following:

boolean done = false;
while (line != null && !done)
{
  System.out.println("S: " + line);
  line = in.readLine()
  if (endResponse(line))
  {
    done = true;
  }
}


Then you just have to write the method endResponse to return true when you get an end tag.  Hope that helps.
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 7122924
Hmm...looking closer at what you said I guess you could change the endResponse() method to check that the response is EITHER "250 OK" OR it does not start with "250".  I think that should do what you want shouldn't it?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 7123324
Ok, looks like I'll just have to look for ways to parse the server's response.  Thanks for the help everyone.

Going to give the points to objects, as he was the first to say that the client could only act on what was being sent by the server.
0
 
LVL 92

Expert Comment

by:objects
ID: 7123334
Have a read of the SMTP protocol:
http://www.ietf.org/rfc/rfc0821.txt

Basically each command sent recieves a reply.
Formally, a reply is defined to be the sequence:  a three-digit code, <SP>, one line of text, and <CRLF>, or a multiline reply (as defined in Appendix E).
0
 
LVL 92

Expert Comment

by:objects
ID: 7123336
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now