[En-Nut-Discussion] Timing and Receiving Data Problem for TCPServer
Jose Vicente Sanchez Ortega
jvsanchezo at gmail.com
Wed Jan 31 11:30:25 CET 2007
I just would like to make a little precision. TCP packets always arrive in
the same order they were delivered. On the other hand, it's UDP which
doesn't make any warranty about this issue (packets can be lose or arrive in
a different order).
De: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] En nombre de Thiago A. Corrêa
Enviado el: martes, 30 de enero de 2007 18:57
Para: Ethernut User Chat (English)
Asunto: Re: [En-Nut-Discussion] Timing and Receiving Data Problem for
I'm sorry if I am wrong, but it seams from the description like a common
misconception about TCP streams. TCP is not packet oriented, it's a stream.
There is no guarantee that you will be able to read all the data you want in
a single read (or NutTcpReceive) call. So you must do subsequent calls. 5000
bytes is larger than the ethernet MTU, so it will be split in at the very
least 3 ethernet frames which can arrive in distict times and even out of
order. Of course this applies to not only ethernut, but anything using TCP.
Instead of inserting a delay, which in many cases can be suficient but
also may not in others, you should check for the amount of data you
received, and if it isn't enought, keep reading until you get it, or you get
a -1, which means the connection was lost.
So, something similar to:
int cnt = 0;
int index = 0;
while ( cnt < 5000 )
if ( cnt = NutTcpReceive( socket, buffer + i, 5000 - i ) < 0 )
break; // lost connection
i += cnt;
instead of simply NutTcpReceive( socket, buffer, 5000 ).
Thiago A. Correa
On 1/30/07, MUHAMMAD AZEEM AZAM <muhammadazeemazam786 at yahoo.com> wrote:
> have to receive data from microcontroller.
> Microcontroller is my server.
> I have a TCP client (Blocking) on PC.
> I have a program that have several buttons.
> when i press one button my client(button has both send and receive
> a) sends data to microcontroller.
> b) microcontroller compares it and then process the request.
> c) if the process is send data back to client it sends me data.
> Now the problem is that when i have to receive 5000 Bytes of data, i
> Receive only 536 Bytes.
> when i use "Sleep(5000)" before Receive function then i get all the data.
> but this delay is too much for me.
> From micrcontroller i am sending the whole (both these techniques)
> a) array of 5000 Bytes
> b) buffer using malloc of 5000 Bytes.
> does anybody has idea how can i solve this problem
> Thanks in Advance for Help
> It's here! Your new message!
> Get new email alerts with the free Yahoo! Toolbar.
More information about the En-Nut-Discussion