bool cWinSock::Receive(char *Data, int &iBytesRecv)
When I return from the function I use this code:
iBytesRecv = Vector.size();
Data = Message;
return (true);
bool cWinSock::Receive(char *Data, int &iBytesRecv)
{
vector<char> Vector;
char szBuffer[1] = {''\0''};
int iError = 0;
// Is the socket connected?
if (!bConnected)
return (false);
// As long there is data waiting on the socket
while (IsData() == SOCK_PENDING)
{
// Get 1 character of the socket
iError = recv(Socket, szBuffer, 1, 0);
// Check if there is a
if (iError <= 0)
{
iBytesRecv = 0;
Data = NULL;
return (false);
}
// Push the character in the vector
Vector.push_back(szBuffer[0]);
}
// Is the message buffer allocated? Delete it if it''s allocated
if (Message != NULL)
{
delete [] Message;
Message = NULL;
}
// Allocate memory for the buffer
Message = new char[Vector.size() + 1];
// Cope the entire vector into the buffer
for (int iIndex = 0; iIndex < Vector.size(); iIndex++)
Message[iIndex] = Vector[iIndex];
// Terminate the message
Message[Vector.size() + 1] = ''\0'';
// Return the data
iBytesRecv = Vector.size();
Data = &Message
return (true);
}
My Site