Weird socket programming java?

B

bigbinc

I have a question and my own answer, but I think it is strange.
I am working with a server, and different clients.

One client is command-line, the other servlet(yea I said servlet, see
tunneling)

The commandline client works fine, but I have to modify my server(not
servlet) to fix the servlet client.

The problem? The servlet client reads in data twice(it is sent twice
or something?). I think it has something to do with the out.flush.

I can remove the newlines below and be ok. I just it is strange.

// server


out.writebytes(bytes);
//out.flush(); ?? remove comments fixes problem strangely
out.writebytes("\r\n\r\n");
out.flush();


// client


public String GetBytes(InputStream _instream) {

String _fin = "";

try {

byte [] tmp = new byte[2048];

int bytes;

int _debugbytes = -1;

while((bytes = _instream.read(tmp)) != -1) {

//_out.write(tmp, 0, bytes);
String _str = new String(tmp);
_fin += _str;

if (bytes > 0)
_debugbytes = bytes;

} // end of thw ehile

} catch(IOException e) {


} // end of the try - catch

return _fin;

} // end of the function

Berlin Brown
http://www.retroevolution.com
(e-mail address removed)
 
G

Gordon Beaton

while((bytes = _instream.read(tmp)) != -1) {

//_out.write(tmp, 0, bytes);
String _str = new String(tmp);

_fin += _str;

You don't consider the byte count when you create _str, so could very
well be reusing "old" data in the buffer. Choose a different String
constructor.

Unrelated to your problem, you should always specify a character
encoding when you convert bytes to Strings. Again, choose a different
String constructor.

Also, you are better off declaring _fin as a StringBuffer, then using
append(_str) after each read. Create a String from the StringBuffer
only after you've finished reading all of the data.

/gordon
 
J

John C. Bollinger

bigbinc said:
I have a question and my own answer, but I think it is strange.
I am working with a server, and different clients.

One client is command-line, the other servlet(yea I said servlet, see
tunneling)

The commandline client works fine, but I have to modify my server(not
servlet) to fix the servlet client.

The problem? The servlet client reads in data twice(it is sent twice
or something?). I think it has something to do with the out.flush.

I can remove the newlines below and be ok. I just it is strange.

Your client code is broken (details below). What you've shown of your
server code is okay either with or without the extra flush, but is
slightly more efficient without. If the command-line client has the
same code as the servlet client then it is also buggy, even if the bug
has not yet manifested there.
// server


out.writebytes(bytes);
//out.flush(); ?? remove comments fixes problem strangely
out.writebytes("\r\n\r\n");
out.flush();


// client


public String GetBytes(InputStream _instream) {

String _fin = "";

try {

byte [] tmp = new byte[2048];

int bytes;

int _debugbytes = -1;

while((bytes = _instream.read(tmp)) != -1) {

//_out.write(tmp, 0, bytes);
String _str = new String(tmp);

The main problem is here. The read does not clear the buffer before
filling it (not necessarilly fully) with data read from the stream, and
neither do you. Therefore, the old data remains and is duplicated to
the string. Moreover, you do not specify the charset with which to
convert the bytes to characters. A correct way to do this is:

String _str = new String(tmp, 0, bytes, "UTF-8");

(Insert the correct charset name. UTF-8 is quite possibly being used as
the default now, but whatever is used now, you may save yourself
headaches later by being explicit. Whatever charset you use here should
also be specified explicitly in the server code.)
_fin += _str;

That's a bit inefficient, but should work.
if (bytes > 0)
_debugbytes = bytes;

} // end of thw ehile

} catch(IOException e) {


} // end of the try - catch

return _fin;

} // end of the function


John Bollinger
(e-mail address removed)
 
B

bigbinc

John C. Bollinger said:
bigbinc said:
I have a question and my own answer, but I think it is strange.
I am working with a server, and different clients.

One client is command-line, the other servlet(yea I said servlet, see
tunneling)

The commandline client works fine, but I have to modify my server(not
servlet) to fix the servlet client.

The problem? The servlet client reads in data twice(it is sent twice
or something?). I think it has something to do with the out.flush.

I can remove the newlines below and be ok. I just it is strange.

Your client code is broken (details below). What you've shown of your
server code is okay either with or without the extra flush, but is
slightly more efficient without. If the command-line client has the
same code as the servlet client then it is also buggy, even if the bug
has not yet manifested there.
// server


out.writebytes(bytes);
//out.flush(); ?? remove comments fixes problem strangely
out.writebytes("\r\n\r\n");
out.flush();


// client


public String GetBytes(InputStream _instream) {

String _fin = "";

try {

byte [] tmp = new byte[2048];

int bytes;

int _debugbytes = -1;

while((bytes = _instream.read(tmp)) != -1) {

//_out.write(tmp, 0, bytes);
String _str = new String(tmp);

The main problem is here. The read does not clear the buffer before
filling it (not necessarilly fully) with data read from the stream, and
neither do you. Therefore, the old data remains and is duplicated to
the string. Moreover, you do not specify the charset with which to
convert the bytes to characters. A correct way to do this is:

String _str = new String(tmp, 0, bytes, "UTF-8");

(Insert the correct charset name. UTF-8 is quite possibly being used as
the default now, but whatever is used now, you may save yourself
headaches later by being explicit. Whatever charset you use here should
also be specified explicitly in the server code.)
_fin += _str;

That's a bit inefficient, but should work.
if (bytes > 0)
_debugbytes = bytes;

} // end of thw ehile
} catch(IOException e) {


} // end of the try - catch

return _fin;

} // end of the function


John Bollinger
(e-mail address removed)


Dope, I knew I had something wrong, I had the idea when I was
printing i.e se the out.write command.

Berlin Brown
http://www.retroevolution.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top