this the client code.server send data continuously.Here I am peeking the 13 (Header Length)byte message getting the size of the data which is encoded in the message itself and creating the buffer size of MESSAGE_HEADER_LENGTH + DATA-LENGTH.
after getting total message size I am using second recv to read data from the socket. during initial connection setup and exchange of hello message between server to client it is working fine but when server sending continuous stream say(when we clieck button at client server send continuous message of fixed lenght with the gap of 5000ms) recv is not working properly and recv buffer receives original message preceded by garbage or some time empty string.
#define MESSAGE_HEADER_LENGTH 13
...
while {
nSelectRetVal = select(NULL, &fdRead,NULL, &fdExcept, &tv);
if(nSelectRetVal > 0) {
if(FD_ISSET(pControlMgr->GetConnectionMgr()->GetUDPSocket(),
&fdRead)) {
try {
char chHeaderBuffer[MESSAGE_HEADER_LENGTH + 1];
memset(chHeaderBuffer,0,MESSAGE_HEADER_LENGTH + 1);
int nMsgPeek = recv(pControlMgr->GetConnectionMgr()->GetUDPSocket(),
chHeaderBuffer, MESSAGE_HEADER_LENGTH, MSG_PEEK);
chHeaderBuffer[MESSAGE_HEADER_LENGTH] = '\0';
if(nMsgPeek == SOCKET_ERROR)
return 0;
CProtocolMgr objProtocolMgr;
int Bufferlength = objProtocolMgr.ProtocolMsgSize(chHeaderBuffer) + MESSAGE_HEADER_LENGTH;
pRecvBuffer = new char[Bufferlength];
memset(pRecvBuffer, 0, Bufferlength);
int nRecvRetVal = recv(pControlMgr->GetConnectionMgr()->GetUDPSocket(),
pRecvBuffer, Bufferlength, 0);
if(nRecvRetVal > 0) {
if(pControlMgr->HandlePacket(pRecvBuffer,
pControlMgr->GetConnectionMgr()->GetServerAddress()) == -1) {
if(NULL != pRecvBuffer) {
delete [] pRecvBuffer;
pRecvBuffer = NULL;
return 0 ;
}
} catch(...) {
if(NULL != pRecvBuffer) {
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
}
}
}
}
}
}