开发者

TCP socket question

开发者 https://www.devze.com 2023-02-07 04:02 出处:网络
I starts learning TCP protocol from internet and having some experiments. After I read an article from http://www.diffen.com/difference/TCP_vs_UDP

I starts learning TCP protocol from internet and having some experiments. After I read an article from http://www.diffen.com/difference/TCP_vs_UDP

"TCP is more reliable since it manages message acknowledgment and retransmissions in case of lost parts. Thus there is absolutely no missing data."

Then I do my experiment, I write a block of code with TCP socket:

while( ! EOF (file))
{
   data = read_from(file, 5KB); //read 5KB from file
   write(data, socket); //write data to socket to send
}

I think it's good because "TCP is reliable" and it "retransmissions lost parts"... But it's not good at all. A small file is OK but when it comes to about 2MB, sometimes it's OK but not always...

Now, I try an开发者_如何学编程other one:

while( ! EOF (file))
{
   wait_for_ACK();//or sleep 5 seconds
   data = read_from(file, 5KB); //read 5KB from file
   write(data, socket); //write data to socket to send
}

It's good now...

All I can think of is that the 1st one fails because of: 1. buffer overflow on sender because the sending rate is slower than the writing rate of the program (the sending rate is controlled by TCP) 2. Maybe the sending rate is greater than writing rate but some packets are lost (after some retransmission, still fails and then TCP gives up...)

Any ideas? Thanks.


TCP will ensure that you don't lose data but you should check how many bytes actually got accepted for transmission... the typical loop is

while (size > 0)
{
    int sz = send(socket, bufptr, size, 0);
    if (sz == -1) ... whoops, error ...
    size -= sz; bufptr += sz;
}

when the send call accepts some data from your program then it's a job of the OS to get that to destination (including retransmission), but the buffer for sending may be smaller than the size you need to send, and that's why the resulting sz (number of bytes accepted for transmission) may be less than size.

It's also important to consider that sending is asynchronous, i.e. after the send function returns the data is not already at the destination, it's has been only assigned to the TCP transport system to be delivered. If you want to know when it will be received then you'll have to use other systems (e.g. a reply message from your counterpart).


You have to check write(socket) to make sure it writes what you ask. Loop until you've sent everything or you've calculated a time out. Do not use indefinite timeouts on socket read/write. You're asking for trouble if you do, especially on Windows.

0

精彩评论

暂无评论...
验证码 换一张
取 消