title: Winsock: How to stop sending/receiving without closing the connection
body: <p>I develop a client/server application which share some camera images. The general cycle is:</p>
  
  <ol>
  <li>The client captures a camera image and compress it to an binary image. After that it sends it to the server</li>
  <li>The server exactly knows how large the buffer is (640*480/8 bytes) so he is able to receive the hole image with only one recv-call. The next recv-call will get the second image and so on</li>
  <li>After receiving the image the server works with the image and will then send another image back to the client. But here is the problem: The image is jpeg compressed and so the client doesn't know the filesize and can't receive the image like the server with a fixed bufferlength.</li>
  </ol>
  
  <p>I think that wouldn't be a problem if the server would close the connection after sending the image. But I don't want to close the connection because in the next cycle I will send the next image over the same connection.</p>
  
  <p>My question is: How can I tell the client that the server send the hole image without closing the connection?</p>
  
  <p>Here's some code:
  (The client sends the  binary image to the server)</p>
  
  <pre><code>void sendToServer(char* sendbuf, int sendbuflen) {
      int iResult = send(ConnectSocket, sendbuf, sendbuflen, 0);
      if (iResult == SOCKET_ERROR) {
          (*errCallbackFunc)("Sending image error", -1);
      }
  }
  </code></pre>
  
  <p>(The server receives the image with only one functioncall - recbuflen = 640*480/8)</p>
  
  <pre><code>int receiveCameraImages(ARUint8* dataPtr) {
  int         iResult;
  
  iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  if (iResult &gt; 0) {
      if ((*(recvbuf+0)) != 'U' || (*(recvbuf+((xsize*ysize/8)-1))) != 'U')
          return RECEIVING_INCOMPLETE;
  
      convertBWChartoBGR(recvbuf, dataPtr, recvbuflen);
  
      return RECEIVING_SUCCEEDED;
  } else if (iResult == 0) {
      (*errCallbackFunc)("Connection closing", -1);
      return RECEIVING_CONN_CLOSED;
  }
  else {
      (*errCallbackFunc)("recv failed", WSAGetLastError());
      return RECEIVING_ERROR;
  }
  }
  </code></pre>
  
  <p>(Now the Server works with the image and send another jpeg compressed image back)</p>
  
  <pre><code>int sendObjectImage(ARUint8* dataPtr, int bufLen) {
  int iResult;
  
  /* send the openGLBuffer back to the client */
  iResult = send(ClientSocket, dataPtr, bufLen, 0);
  if (iResult == SOCKET_ERROR) (*errCallbackFunc)("send openglbuffer back to client failed", WSAGetLastError());
  }
  </code></pre>
  
  <p>(And the client doesn't know how large the buffer is so it receives 512Byte parts)</p>
  
  <pre><code>int receiveFromServer(ARUint8* dataPtr) {
  int iResult, returnBufLen = 0;
  int recvBufLen = DEFAULT_BUFFER_LEN;
  ARUint8 recvBuf[DEFAULT_BUFFER_LEN];
  
  /* receive openGL buffer */
  do {
      iResult = recv(ConnectSocket, recvBuf, recvBufLen, 0);
      if (iResult &gt; 0) {
          printf("Bytes received: %d\n", iResult);
          returnBufLen += iResult;
      }
      else if (iResult == 0)
          printf("Connection closed\n");
      else
          printf("recv failed: %d\n", WSAGetLastError());
  } while (iResult &gt;= DEFAULT_BUFFER_LEN);
  
  dataPtr = recvBuf;
  
  return returnBufLen;
  }
  </code></pre>
  
  <p>with the while (iResult >= DEFAULT_BUFFER_LEN) I thought it will stop the receiving (because if a part is smaller than 512Bytes it must be the end). But my recvBuf only contains the last Bytes.</p>
  
  <p>Maybe it's possible that I don't understand the hole winsock stuff, but I thought that the recvBuf will contain the hole buffer I received. Is that wrong? How do I get the hole received buffer without closing the connection?</p>