I'm trying to get one side to send an error message to client, but client isn't able to parse it correctly.
My error is >>>>> in my parseString function, it lets index = 0 and therefore I get an out of range for my 'substr' call.
Server Side:::
#define ERRBUFSIZE 51
string error = "Error - Already Registered: ";
error.append(name);
char err[ERRBUFSIZE];
err[0] = 0;
std::copy(error.begin(), error.end(), err+1);
err[error.size() + 2] = '\0';
if (send(sock, &err, ERRBUFSIZE, 0) < 0)
{
DieWithError("send() failed");
}
Client side ( who is receiving )::
char msgBuffer[ERRBUFSIZE];
int bytesRcvd = 0;
int totalRcvd = 0;
while(totalRcvd != ERRBUFSIZE)
{
// Get Message from Server
if ((bytesRcvd = recv(sock, msgBuffer, ERRBUFSIZE, 0)) < 0)
DieWithError("recv() failed or connection closed prematurely");
totalRcvd += bytesRcvd;
}
cout << "Bytes received: " << totalRcvd << endl;
msgBuffer[totalRcvd] = '\0';
string rcvMsg( msgBuffer , msgBuffer + totalRcvd);
cout << rcvMsg << endl;
rcvMsg = parseString(rcvMsg);
return rcvMsg;
where....
string TCPClient::parseString(string message)
{
cout << "parsing new string" << endl;
int index = message.find('\0');
string result = message.substr(0, index);
cout << "THE RESULT :: " << result << endl;
return result;
}