简体   繁体   中英

TCP Packet size

Im trying to send a TCP packet from client to server, I want to be able to select the size of the packet. The problem Im facíng is that the packet size is not correct if I dont put a Sleep(45) (milliseconds). Im using Wireshark to see the size of the packets.

To make sure u guys are with me Im going to explain as clear as possible.

I have tried to do like this..

First I select the amount a data I want to send. For example say 1Mb or "1000000ish"bytes. I allocate an array with so much space.

To be able to send a specific packet size I have allocate a sendbuffer which contains the size of the packet I want (my case 64, 512, 1024 and 1514 bytes). I fill the sendbuffer with letters. Say I want to send with 64 as packet size.

for (int i = 0; i < packetSize; i++){ 
            sendbuf[i] = 'a';
        }

To know how many times I have to send a packet to reach 1Mb I done this math.
nrOfTimes = (dataSize / packetSize).

Then to send it with a loop.

for (int i = 0; i < nrOfTimes; i++) {
            rc = send(sConnect, sendbuf, packetSize, 0); //rc and sConnect contains information where to send the data, if you wonder

            Sleep(45); // If i dont use this the packet size gets 1514. 
        }

If I use the sleep(45) its working but its takes years to finished and Im supposed to measure the time so its incorrect to do like this. if I go lower then Sleep(45) then my network card ignors the packets size and put it to 1514 size.

Is there anyone who has some clear ideas what to do? I can only assume it might have something to do with the network card buffer.

TCP is a byte streaming protocol so it is incorrect to think of transmission in terms of discrete packets of bytes. It is likely you are interacting with the Nagle algorithm by injecting the 45 ms delays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM