Quick Sockets Programming Question

CubanCorona

Senior member
Jul 13, 2001
258
0
0
Suppose you are using java sockets to send data via TCP to a remote host. If you call the send() method multiple times, TCP will aggregate your messages into a single packet. My question is this: If you send a short message (much less than MTU) using send(), are there any circumstances in which a single reveive() on the remote host will not receive the entire message (assuming the receive buffer is of adequate size)?

e.g.

host1
send("hello there")

host2
1st receive() = "hello "
2nd receive() = "there"

I've never seen this occur in practice, but it seems like it could occur if the first part of the message fills up a packet that has been aggregating data from the last few sends, so the end of the message has to be sent in a new packet.

Thanks
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I assume receive tells you how many bytes it was able to receive, so you really should just call receive in a loop until it returns 0 or use something like select to see if there's any data to be read.
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
First of all you will get a better response in the software forum. You have run into the classic problem with TCP messaging. You must reconstruct the message on the receiver side, which could mean multiple reads. You also can run into a problem where the end of message comes in with start of the next message. In this case you have to break it in two. I have a TCP message library that resolves all of these issues... If only I could remember where I saved it.