Solved

problem with I/O

Posted on 2002-06-30
14
214 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

708 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

20 Experts available now in Live!

Get 1:1 Help Now