[En-Nut-Discussion] Timing and Receiving Data Problem for TCP Server
Thiago A. Corrêa
thiago.correa at gmail.com
Tue Jan 30 18:57:13 CET 2007
Hi,
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 ).
Regards,
Thiago A. Correa
On 1/30/07, MUHAMMAD AZEEM AZAM <muhammadazeemazam786 at yahoo.com> wrote:
>
> Hi
> I
> 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
> functions)
> 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
> Bye
>
>
>
> ---------------------------------
> It's here! Your new message!
> Get new email alerts with the free Yahoo! Toolbar.
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
>
More information about the En-Nut-Discussion
mailing list