Re: data packet split problem in socket networking

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Vic" <Vic@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
Thanks Ken! I am using TCP.

Could anyone please give me detail answer to the following questions?

1. The socket split can be avoided or not?

You should allow for the split in your receiver code. In general it can not
be avoided.

2. What should be the suitable packet size be set?

See answer to Q1.

3. Is it for sure that if a packet is split, the data will be sent in the
succeeded packet? We should be sure that the data should be
received in order.

The data will be received in order.

4. Will the socket packet be split and re-organized? For example, if the
previous packet is split, will the next socket packet contain the rest
part
of the previous packet plus some of the data from the next packet?

The next 'socket packet' may, in general, contain just one byte, or
everything that the sender has sent since the last 'socket packet' you
received.

If it is true, the header info may not be at the beginning of the packet.

That is true.

5. Generally, where in a packet will the split happen? Are there any rules
for this?

None that your receive code should rely on.

6. The SourceID takes 2 bytes in our case, is it possible that the split
happens exactly between the the 2 bytes of SourceID, say the 1st byte of
SourceD is in one packet, and the 2nd byte of SourceID is sent in the
another
packet (hope it is in the next packet)?

Yes that's possible.

If this happens, it will be even more difficult for me to process the
received data.

It's not difficult if you forget about the concept of receiving data in the
same size chunks that you send.

Think how you would do it if you were reading the data from a file, for
example.

Typically, it's necessary to have some kind of a header on the data, with at
least a length field.

The receive code could be a little state machine, knowing what to expect
next, based on what it's already received.

For example, say you had a header with a 2 byte length and a 2 byte 'message
id'.

Code would be

recv_n_bytes(4, ...)
Get length out of header
recv_n_bytes(length, ...)

recv_n_bytes() is just a wrapper for recv(), which does not return until the
requested number of bytes has been received, or there is a recv() error.

HTH.

Dave.

Please tell me the answer or tell me the souce where i can find more info.

Many thanks!

Vic


"Skywing [MVP]" wrote:

If you are using TCP, be aware that one call to send() may be perceived
as
multiple different calls to recv() on the other end (or vice versa -
multiple send() calls can be received in one recv() call). There is also
the possibility that you will get a partial message in a call to recv(),
requiring you to make additional calls.

TCP guarantees that your data will arrive in the same order.

UDP will guarantee that you only receive whole messages, but it does not
guarantee reliability nor does it guarantee sequencing. I assume that
you
are probably using TCP, since you spoke of messages appearing to be
split.

--
Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Vic" <Vic@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DC75258F-2EB9-4929-A9D7-A86A16650827@xxxxxxxxxxxxxxxx
Hi Guru,

I am new with socket applications, and now trying to build a
client-server
application to use socket for data transportation.
I am using VC6 on PC, included ws2_32.lib in my project.

I found that *sometimes* the packet received at the client side is
split
and
MAYBE *re-organized* (I am sure the packets were often split, but need
to
confirm if the packets are re-organized or not).
I have several data sources, e.g., each data source is a file, to be
sent
to
the SAME client.
One way for the implementation is: for each data source, we build a
socket,
and send its data to the client at a unique port.
This will make the different data sources independent, but needs many
connections.

Another way is just to use one Server-Client connection, and the server
sends the data from each source one by one.
Each packet of data has a header, which indicates to which data source
this
packet belongs, e.g., SourceID.
Hopefully, the server will receive the packets of data in exactly the
same
packet as the client sent it.
But if the packets are split and re-organized, special process will be
needed to separate the data in the receiver buffer.

I have following questions and hope you can give me the answers:

1. The socket split can be avoided or not? Say, if we use smaller
packet
size, will we be sure that the socket will not be split?
2. What should be the suitable packet size be set?
3. Is it for sure that if a packet is split, the data will be sent in
the
succeeded packet? We should be sure that the data should be received in
order.
4. Will the socket packet be split and re-organized? For example, if
the
previous packet is split, will the next socket packet contain the rest
part
of the previous packet plus some of the data from the next packet?
If it is true, the header info may not be at the beginning of the
packet.
5. Generally, where in a packet will the split happen? Are there any
rules
for this?
6. The SourceID takes 2 bytes in our case, is it possible that the
split
happens exactly between the the 2 bytes of SourceID, say the 1st byte
of
SourceD is in one packet, and the 2nd byte of SourceID is sent in the
another
packet (hope it is in the next packet)? If this happens, it will be
even
more
difficult for me to process the received data.

Please tell me the answer or tell me the souce where i can find more
info.

Thanks in advance!

Victor




.



Relevant Pages