Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with recv() on a tcp connection
    primarykey
    data
    text
    <p>I am simulating TCP communication on windows in C. I have sender and a receiver communicating.</p> <p>The sender sends packets of specific size to the receiver. The receiver gets them and sends an ACK for each packet it received back to the sender. If the sender didn't get a specific packet (they are numbered in a header inside the packet) it sends the packet again to the receiver. Here is the getPacket function on the receiver side:</p> <pre><code>//get the next packet from the socket. set the packetSize to -1 //if it's the first packet. //return: total bytes read // return: 0 if socket has shutdown on sender side, -1 error, else number of bytes received int getPakcet(char* chunkBuff, int packetSize, SOCKET AcceptSocket) { int totalChunkLen = 0; int bytesRecv = -1; bool firstTime = false; if(packetSize == -1) { packetSize = MAX_PACKET_LENGTH; firstTime = true; } int needToGet = packetSize; do { char* recvBuff; recvBuff = (char*)calloc(needToGet, sizeof(char)); if(recvBuff == NULL) { fprintf(stderr, "Memory allocation problem\n"); return -1; } bytesRecv = recv(AcceptSocket, recvBuff, needToGet, 0); if(bytesRecv == SOCKET_ERROR) { fprintf(stderr, "recv() error %ld.\n", WSAGetLastError()); totalChunkLen = -1; return -1; } if(bytesRecv == 0) { fprintf(stderr, "recv(): socket has shutdown on sender side"); return 0; } else if(bytesRecv &gt; 0) { memcpy(chunkBuff + totalChunkLen, recvBuff, bytesRecv); totalChunkLen += bytesRecv; } needToGet -= bytesRecv; } while((totalChunkLen &lt; packetSize) &amp;&amp; (!firstTime)); return totalChunkLen; } </code></pre> <p>I use <code>firstTime</code> because for the first time the receiver doesn't know the normal package size that the sender is going to send to it, so I use a <code>MAX_PACKET_LENGTH</code> to get a package and then set the normal package size to the number of bytes I have received.</p> <p>My problem is the last package. It's size is less than the package size. So lets say last package size is 2 and the normal package size is 4. So <code>recv()</code> gets two bytes, continues to the while condition, then <code>totalChunkLen &lt; packetSize</code> because <code>2&lt;4</code> so it iterates the loop again and the gets stuck in <code>recv()</code> because it's blocking because the sender has nothing to send.</p> <p>On the sender side I can't close the connection because I didn't get ACK back, so it's kind of a deadlock. The receiver is stuck because it's waiting for more packages but sender has nothing to send.</p> <p>I don't want to use a timeout for <code>recv()</code> or to insert a special character to the package header to mark that it is the last one.</p> <p>What can I do?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload