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 another 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.
write()has three arguments andwait_for_ACK()andread_from()do not exist in POSIX. This is more like pseudo-code from somewhere. Care to show us the actual code that you tried?