Skip to main content

Getting data from server

3 replies [Last post]
quo
Offline
Joined: 2008-07-13
Points: 0

I'm implementing program as shown in article
http://developers.sun.com/mobility/midp/articles/midp2network/.

Part of the code that is bothering me is in section Code Sample 4: EmailClient.java

is = sc.openInputStream();
int c = 0;
while (((c = is.read()) != -1) ) {
sb.append((char) c);
}

To clear that out, I read that code like this: read for any byte on input stream until there is nothing more to read.

On the server side (server is not on a mobile device, but on PC) I just put dot (.) on the text sent by MIDlet and sending it back, as show here:

outputLine = inputLine + "!" + null;
out.println(outputLine);

To thing I cannot figure out is that code on mobile device loop throug stream sent by server and when it read the last sign it stops on while (((c = is.read()) != -1) ) { and waits for another sign. Why?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15
Points: 0

You need to close the output stream that is writing to the inputstream that is blocking on the read.

quo
Offline
Joined: 2008-07-13
Points: 0

But when I close stream, I am closing stream on the server side too. That way I have to constantly create connection between midlet and server.
Is there solution to keep stream, better to say, connection all the time?

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Oh sure there is a way to keep constant connection between the device and the server.
Your question was why did the while( (c = is.read()) != -1)... code never get a -1 condition.

What you want to do is setup a protocol that you can monitor for a known end state.
So in a email client commands have a newline to determine the completion of a response.
When sending text from a message that might have embedded newlines I think there was a
period on a separate line.
Here is the link to the Email RFC: http://www.ietf.org/rfc/rfc1939.txt

So figure out a protocol, either binary, or textual (FYI.. Binary will be smaller) then in your while loop look for your end of state marker, change the state then start waiting for the start of the next state.

-Shawn