When I want to send a message from server, everything works fine, e.g. I send Message "hello client" and the client receives message "hello client", BUT when I send the message from a client to the server, the server receives weird message, such as "hello server" and "đş" to fill the rest of the buffer (char cTextBuffer[102400];)
send function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void PathSocket::PacketSend(int iSendTo, const char* buff, int len, int flags)
{
if(iType == SOCKET_TYPE_CLIENT)
send(mainSock, buff, len, flags);
else if(iType == SOCKET_TYPE_SERVER)
{
if(iSendTo == SOCKET_SEND_ALL)
{
if(iConnectedSockets == 0)
return;
else if(iConnectedSockets == 1)
send(sock[0], buff, len, flags);
else
{
for(int i = 0; i < iConnectedSockets; i++)
send(sock[i], buff, len, flags);
}
}
else
send(sock[iSendTo], buff, len, flags);
}
else
return;
}
|
Server sending:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
case IDC_SEND:
{
/*HWND hText = GetDlgItem(hWnd, IDC_MSG);
char cMsg[64];
SendMessage(hText, WM_GETTEXT, 64, (LPARAM)cMsg);*/
char cMsg[64];
GetDlgItemText(hWnd, IDC_MSG, cMsg, sizeof(cMsg)/sizeof(cMsg[0]));
ps->PacketSend(SOCKET_SEND_ALL, cMsg, strlen(cMsg), NULL);
break;
}
|
Client Sending:
1 2 3 4 5 6 7 8 9 10
|
case IDC_SEND:
{
char cMsg[64];
GetDlgItemText(hWnd, IDC_MSG, cMsg, sizeof(cMsg)/sizeof(cMsg[0]));
ps.PacketSend(SOCKET_SEND_ALL, cMsg, strlen(cMsg), NULL);
break;
}
|
as you can see ,those 2 are totally identical, and I'm even using the same function to receive the message, on FD_READ or when receiving something fro mserver on the client there's this function called:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int PathSocket::PacketReceive(HWND hWnd, SOCKET socket)
{
int size;
if((size = recv(socket, cTextBuffer, BUFSIZE, 0)) == SOCKET_ERROR)
{
WSAAsyncSelect(socket, hWnd, 0, 0);
strncpy_s(cTextBuffer, "Error while receiving data!", BUFSIZE);
send(socket, cTextBuffer, strlen(cTextBuffer), 0);
shutdown(socket, SD_BOTH);
closesocket(socket);
return SOCKET_ERROR;
}
for(int i = 0; i < iConnectedSockets; i++)
{
if(sock[i] == socket)
{
iLastSocket = i;
break;
}
}
return FD_READ;
}
|
BUT the result is totally different
server sending to client:
http://filebeam.com/2a9cb99cfda770cd86691e8e22db10d9.jpg
client sending to server:
http://filebeam.com/ad5a4dabafd1f86befb4a911d8624d24.jpg
when the socket receives a message, it stores it in the cTextBuffer array, and then I can return it with function PathSocket::GetBuffer(), I'm using the SAME code to SEND AND RECEIVE the messages in both applications