Hi All,
my client server application can work in two modes:
1) one direction - only client sends msgs to server
2) two directions - server gives 'answers' to client.
when program run in the first mode it looks OK, but when server answers to client than client's application exit its operation after a short while. I am trying to find the problem but have some taugh time.
this is the function that reads the data in the client side (in which I have problems):
Code:
int TCPClient::readSocketData(int s,
char *decodeUnifiedMsgForServer,
int n,
bool& isMsg
)
{
int bcount; int br;
bcount = 0;
br = 0;
while (bcount < n)
{
if ((br = recv(s,decodeUnifiedMsgForServer,n-bcount,0)) > 0)
{
isMsg = true;
bcount += br; decodeUnifiedMsgForServer+= br;
}
else if (br < 0) /* signal an error to the caller */
{
return(-1);
}
else {
return bcount;
}
//Y 17_04_05 -
Sleep(0);
//Y.
}
return(bcount);
}
after buffer (decodeUnifiedMsgForServer) is filled by the function above I decode the data into the relevant data classes as follows:
Code:
void BrainControlComData::decodeMsg(char* decodeUnifiedMsgForServer)
{
if(strlen(decodeUnifiedMsgForServer) == 0)
{
char* error = "probably an error (see Q1 below)";
}
char* msg = decodeUnifiedMsgForServer;
int size = 4;
int lenCursorData;
int lenManipData;
int lenVzData;
//places 1 - 12 in msg are reserved for total size/ isEventMsg/ MsgID
int Web_lenOfClass;
// decode len of msg
memcpy(&Web_lenOfClass, msg, size);
int lenOfClass = ntohl(Web_lenOfClass);
int Web_MsgID;
// decode msg ID
memcpy(&Web_MsgID, msg + MSG_ID_LOCATION, size);
int msgID = ntohl(Web_MsgID);
//Y 6_07_05 -
int counter = 12;
int len = 0;
len = comData[CURSOR_DATA]->decodeMsg(msg + counter);
counter += len;
len = comData[STATE_DATA]->decodeMsg(msg);
}
the first 3 bytes in the buffer are integers (general data such as - total len, msgID etc.) and I decode them in the 2nd function. afterwards I send the buffer into specific data classes that decode the relevant data they need using the fuction decodeMsg.
my questions are as follows:
1) why is the length of the buffer (decodeUnifiedMsgForServer) is always 0 and not equals to the length of the msg received from server? I know that the data is received from the server because it is logged correctly, but still when I check length of msg it equas 0 (first 'if' in second function) - any ideas?
2) when I remarked the line that calls the decodeMsg the application 'behaved' OK and client didn't exit its operation. I think that this error has something to do with memory leak. maybe if I understood the first question I could understand this one as well.
Thank you so much for your help,
Lenna