E
edsionchen002
Unregistered / Unconfirmed
GUEST, unregistred user!
typedef struct { unsigned short usType; unsigned short usLength;} ST_PUSHHEAD;void handlePushRecv(SOCKET sock){ ST_PUSHHEAD stPushHead; // push header int nHeaderLen = sizeof(ST_PUSHHEAD); // push header length int nDataLen = 0; // recv data length int s_nReceived = 0; // static para int nType; // command type int nRead = 0; // counter of recv char char szRecvBuffer[MSG_MAXLEN] = ""; // recv buffer // begin recv || just recv a part of header if (s_nReceived < nHeaderLen) { nDataLen = 0; nRead = ::recv (sock, (char *) &stPushHead + s_nReceived, nHeaderLen - s_nReceived, 0); if (nRead <= 0) { // sock error occurs, must close if (GetLastError() != WSAEWOULDBLOCK) { s_nReceived = 0; return; } } s_nReceived += nRead; if (s_nReceived < nHeaderLen) return; nDataLen = ntohs(stPushHead.usLength); nType = ntohs(stPushHead.usType); } if (nDataLen > MSG_MAXLEN || nDataLen < 0) { s_nReceived = 0; return; } // just recv a part of whole pack if (s_nReceived < nHeaderLen + nDataLen) { nRead = ::recv (sock, szRecvBuffer + s_nReceived - nHeaderLen, nDataLen + nHeaderLen - s_nReceived, 0); if (nRead <= 0) { if (GetLastError() != WSAEWOULDBLOCK) { s_nReceived = 0; return; } } s_nReceived += nRead; } // recv whole pack if (s_nReceived == nHeaderLen + nDataLen) { // print message printf ("receive from server: [%d]%s/n", nType, szRecvBuffer); // reset param s_nReceived = 0; nDataLen = 0; memset(szRecvBuffer, 0, sizeof(MSG_MAXLEN)); }}