Note that there are some explanatory texts on larger screens.

plurals
  1. POC C++ - TCP Socket Class : Receive Problem
    text
    copied!<p>I did my own Socket class, to be able to send and receive HTTP requests. But I still got some problems. The following code (my receive function) is still buggy, and crashing sometimes. I tried debugging it, but it must be somewhere in the pointer arithmetics / memory management.</p> <pre><code>int Socket::Recv(char *&amp;vpszRecvd) { //vpszRecvd = NULL; int recvsize = 0; char TempBuf[1024]; int Result = 0; char* temp; do { memset(TempBuf, 0, sizeof(TempBuf)); Result = recv( this-&gt;sSocket, TempBuf, sizeof(TempBuf) -1, 0 ); if (recvsize == 0) recvsize = Result; if ( Result &gt; 0 ) { if ( vpszRecvd != NULL ) { if (temp == NULL) { temp = (char*)calloc(recvsize + 1, sizeof(char)); } else { realloc(temp, recvsize + 1); } if (temp == NULL) return 0; memcpy(temp, vpszRecvd, recvsize); realloc(vpszRecvd, recvsize + Result); if (vpszRecvd == NULL) return 0; memset(vpszRecvd, 0, recvsize + Result); memcpy(vpszRecvd, TempBuf, Result); memcpy(vpszRecvd + recvsize, TempBuf, Result); recvsize += Result; } else { realloc(vpszRecvd, Result); if (vpszRecvd == NULL) return 0; memset(vpszRecvd, 0, Result); memcpy(vpszRecvd, TempBuf, Result); recvsize += Result; } } else if ( Result == 0 ) { return recvsize; } else //if ( Result == SOCKET_ERROR ) { closesocket(this-&gt;sSocket); this-&gt;sSocket = INVALID_SOCKET; return SOCKET_ERROR; } } while( Result &gt; 0 ); return recvsize; } </code></pre> <p>Does anybody see anything that could cause the crash, or does anyone have a better / faster / smaller and stable example how to receive a full packet via recv()?</p> <p>I can't use strings, it must be done with chars, though.</p> <p>Thanks for your help.</p>
 

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