Trouble@Mill said:
Trouble@Mill wrote On 02/02/06 17:07,:
In response to Eric and Lothar:
[...]
00000720: 6669 6C65 206E 616D 653D 222E 6D61 696C 'file name=".mail'
00000730: 6361 7022 2074 7970 653D 2266 696C 6522 'cap" type="file"'
00000740: 202F 3E3C 2F63 7572 7265 6E74 3E3C 2F64 ' /></current></d'
00000750: 6972 6563 746F 7279 3E0A 'irectory>. '
Feb 02 13:34:38.642 Timed out waiting for reply
Feb 02 13:34:38.642 Got back: []
Feb 02 13:34:38.642 recv 0
Data:
00000000: ' '
TCP Socket closed 1888
From this, you can see that the returned data was not received in a
single buffer, and that the recv() DID understand that the data was
complete, and posted a 0 length reply to indicate this.
No; recv() did not understand that the "data was
complete." Rather, select() understood that there was
a ten-second interval of silence; after select() timed
out, recv() wasn't even called again.
Sorry Eric, you missed some pieces in your reply:
Feb 02 13:34:28.617 bytesReceived 0
Feb 02 13:34:28.617 Got back: [<directory><current
[..]
Feb 02 13:34:28.617 recv 1882
Data:
00000000: 3C64 6972 6563 746F 7279 3E3C 6375 7272 '<directory><curr'
[..]
00000750: 6972 6563 746F 7279 3E0A 'irectory>. '
Feb 02 13:34:38.642 Timed out waiting for reply
From that you can see that the data was received and printed BEFORE
the select() was re-entered, not after the select() timed out.
Cheers,
Eddie
It seems to me that recv returned immediately when there were 0 bytes
available for reading, since you do a non-blocking read. You are just
lucky that this occurs at the right time in your C code, as opposed to
your Java code. Your assumption that that a 0 byte read indicates the
end of the "message" is invalid, as mentioned numerous times already, it
simply indicates that there was nothing ready at that particular time.
I'd suggest doing something like:
// write your message to the server
OutputStream os = socket.getOutputStream();
os.write(message.getBytes());
os.flush();
InputStream is = socket.getInputStream();
// read the response from the server
byte[] buff = new byte[1024];
int got;
boolean complete = false;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((got=is.read(buff))>=0 && ! complete) {
baos.write(buff,0,got);
// this is VERY inefficient, a better way would be to maintain
// some kind of state machine, or possibly use SAX to parse
// the XML as it comes in, so you know when you end the response.
if (new String(baos.getBytes()).endsWith("</directory>"))
complete = true;
}
// process the message
Regards,
Rogan