Skip to main content

Getting data from server

3 replies [Last post]
Joined: 2008-07-13

I'm implementing program as shown in article

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

is = sc.openInputStream();
int c = 0;
while (((c = != -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;

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 = != -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.
Joined: 2003-06-15

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

Joined: 2008-07-13

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?

Joined: 2003-06-15

Oh sure there is a way to keep constant connection between the device and the server.
Your question was why did the while( (c = != -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:

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.