Number of bytes when receiving packets using C++

J

jaco.versfeld

Hi There,

I have a basic TCP client and TCP server in C++. The TCP client
connects to the server, and after a setup phase starts to transmit a
file to the TCP server using multiple packets (fixed amount of bytes,
except for the last packet). This is accomplished by using a for loop
and the "int send(socket, buffer, bufferlength, 0)" function.

When I read the packets using "int recv(socket, buffer, 1500, 0)" the
number returned by the function is 1500, although the "data" packets
that I have sent each contains less bytes (1310 bytes). It seems that
with each recv() function, I get 1500 bytes from the network buffer,
and that I have lost "synchronisation" - I don't read packet for
packet, but a chunk of the network buffer. Is this the normal
operation of TCP, or did I break something along the way? (I would
hope that each recv function would return 1310, the size of the
original packet.)

Any help, suggestions and comments will be greatly appreciated
Jaco
 
S

Stefan Naewe

Hi There,

I have a basic TCP client and TCP server in C++. The TCP client
connects to the server, and after a setup phase starts to transmit a
file to the TCP server using multiple packets (fixed amount of bytes,
except for the last packet). This is accomplished by using a for loop
and the "int send(socket, buffer, bufferlength, 0)" function.

When I read the packets using "int recv(socket, buffer, 1500, 0)" the
number returned by the function is 1500, although the "data" packets
that I have sent each contains less bytes (1310 bytes). It seems that
with each recv() function, I get 1500 bytes from the network buffer,
and that I have lost "synchronisation" - I don't read packet for
packet, but a chunk of the network buffer. Is this the normal
operation of TCP, or did I break something along the way? (I would
hope that each recv function would return 1310, the size of the
original packet.)

This is pretty OT in comp.lang.c++. Ask in comp.unix.programming for example.

But I tell you:
TCP is stream based. There are now record borders (you'd use UDP
for that). You have to transmit the record size in your packet
structure.

Regards,
Stefan
 
I

Inso Reiges

Hi There,

I have a basic TCP client and TCP server in C++. The TCP client
connects to the server, and after a setup phase starts to transmit a
file to the TCP server using multiple packets (fixed amount of bytes,
except for the last packet). This is accomplished by using a for loop
and the "int send(socket, buffer, bufferlength, 0)" function.

When I read the packets using "int recv(socket, buffer, 1500, 0)" the
number returned by the function is 1500, although the "data" packets
that I have sent each contains less bytes (1310 bytes). It seems that
with each recv() function, I get 1500 bytes from the network buffer,
and that I have lost "synchronisation" - I don't read packet for
packet, but a chunk of the network buffer. Is this the normal
operation of TCP, or did I break something along the way? (I would
hope that each recv function would return 1310, the size of the
original packet.)

Any help, suggestions and comments will be greatly appreciated
Jaco

This is OT here, you would probably get better answers elsewhere.
But, as far as i know, tcp read\write is streamed. The way, i would
go, is design a simple protocol on top of TCP with a field for record
lenght.
 
D

David Schwartz

I have a basic TCP client and TCP server in C++. The TCP client
connects to the server, and after a setup phase starts to transmit a
file to the TCP server using multiple packets (fixed amount of bytes,
except for the last packet). This is accomplished by using a for loop
and the "int send(socket, buffer, bufferlength, 0)" function.

Your bad terminology is going to cause you no end of pain. You are
using the term "packets" when you mean "records".
When I read the packets using "int recv(socket, buffer, 1500, 0)" the
number returned by the function is 1500, although the "data" packets
that I have sent each contains less bytes (1310 bytes).

You are probably sending 1,310 byte records in 1,500 byte packets (or,
more precisely, packets that hold up to 1,500 bytes of application
data).
It seems that
with each recv() function, I get 1500 bytes from the network buffer,
and that I have lost "synchronisation" - I don't read packet for
packet, but a chunk of the network buffer. Is this the normal
operation of TCP, or did I break something along the way?

If you call 'recv' fast enough, and no packets are lost, you *will*
receive packets. However, packets are not records.
(I would
hope that each recv function would return 1310, the size of the
original packet.)

Except that wasn't the size of the original packet, that was the size
of the original record.
Any help, suggestions and comments will be greatly appreciated

Don't call a record a packet. Sloppy terminology is your main problem.

You are sending records and receiving data as it becomes available
(which is going to be in chunks of packets).

DS
 
N

Nick Keighley

On Jun 6, 6:38 am, (e-mail address removed) wrote:

Your bad terminology is going to cause you no end of pain. You are
using the term "packets" when you mean "records".

or go the whole hog and call 'em PDU's

:)

<snip>
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top