Problems with server communicaton

Kirby

Lifer
Apr 10, 2006
12,028
2
0
I'm writing a program to find the date and from fields from our school's inbox in Java. I've done the project before in C++, but I'm having trouble reading the response from the server.

I just get the first line, then it eventually times out. It's not a server problem, because I can telnet it and go through the protocol just fine.

Socket s = new Socket(args[0], 143);
InputStream inStream= s.getInputStream();
OutputStream outStream = s.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream,true);

// Protocol - step #1 - read the initial greeting message from the server

while(in.hasNextLine()){response=in.nextLine(); System.out.println(response);}
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: nkgreen
Nevermind, I just removed the while loop and it works fine. I don't understand why, but whatever. You'd think that if it read just one line it would still end, but I guess not.

Don't you have to read a line before you can check if the input stream has another line? You don't do that in the code in your OP.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I was going to say the same thing, but I checked the Sun doc's and apparently the hasNextLine() returns true when it has another line, but if it doesn't have another line it will block until it does. Kind of unclear really.
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Grr. Not working. If I have it in a loop it doesn't end, and if I have it once it only prints the first line. I tried reading a line, then the loop but the same thing happens.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You might try using hasNext instead of hasNextLine

It could have something to do with newline characters.
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Bah, what a pain in the ass. I've got to read until I come to a certain string in the response. His lecture notes made it sound like it would stop when there were no more lines, but apparently not.

A number of students have asked this question, so I thought I would provide a general comment on it rather than just tell those students who have asked.

In project five you need to communicate with an IMAP server on port 143. You carry out a conversation where you say "login" and the server responds and then "select" and the server responds and then "fetch" and so on.

When we wrote this program in C++, the server simply sent us a huge block of text and you parsed through it (finding various fields and determining where the end of line was for that specific field).

In Java, if you use the I/O that we looked at in class (setting up a Scanner based on an InputStream from the socket), then a call to "nextLine" simply returns part of the response from a given command (the next line of the response).

If you do the communication by hand (telnet to port 143 on bama), you notice that commands like SELECT and FETCH return multiple lines. You need to be able to read all of these lines. The trick is determining when to quit reading lines of response.

Notice that the response to a SELECT request ends with a line that reads "aNNN OK ...... SELECT completed"

Also notice that the response to a FETCH command ends with a line that reads "aNNN OK FETCH completed"

So in both cases you need to be able to read lines of response from the server until you see the "....OK.... completed" line.
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Here's what ended up working.

response=in.nextLine(); System.out.println(response);
int number_messages=num_messages(response);
while(!response.contains("completed")){response=in.nextLine(); System.out.println(response);}