?
Solved

problem with I/O

Posted on 2002-06-30
14
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 150 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
 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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