typedef struct {
LONG32 msgLen;/* Message length, excluding this field */
BYTE record [MAX_MSG_LEN];
} MESSAGE;
Note
The message length variables are fixed-precision LONG32
type to remind readers that the length, which is included in messages
that may be transferred to and from programs written in other languages
(such as Java) or running on other machines, where long integers may be
64 bits, will have a well-defined, unambiguous length.
DWORD ReceiveMessage (MESSAGE *pMsg, SOCKET sd)
{
/* A message has a 4-byte length field, followed
by the message contents. */
DWORD disconnect = 0;
LONG32 nRemainRecv, nXfer;
LPBYTE pBuffer;
/* Read message. */
/* First the length header, then contents. */
nRemainRecv = 4; /* Header field length. */
pBuffer = (LPBYTE) pMsg; /* recv may not */
/* Receive the header. */
while (nRemainRecv > 0 && !disconnect) {
nXfer = recv (sd, pBuffer, nRemainRecv, 0);
disconnect = (nXfer == 0);
nRemainRecv -=nXfer; pBuffer += nXfer;
}
/* Read the message contents. */
nRemainRecv = pMsg->msgLen;
/* Exclude buffer overflow */
nRemainRecv = min(nRemainRecv, MAX_RQRS_LEN);
while (nRemainRecv > 0 && !disconnect) {
nXfer = recv (sd, pBuffer, nRemainRecv, 0);
disconnect = (nXfer == 0);
nRemainRecv -=nXfer; pBuffer += nXfer;
}
return disconnect;
}