[Win32Api] Sending buffer over socket with 2 different results, same code

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
they're not the same:
ps->PacketSend(SOCKET_SEND_ALL, cMsg, strlen(cMsg), NULL);

ps.PacketSend(SOCKET_SEND_ALL, cMsg, strlen(cMsg), NULL);
right, it works when I do it without a pointer, BUT I need to create the server using the pointer, so how do I fix this? I rarely use pointers, so I dont have much experience in this area

edit:

when creating a server, before I initialize sockets and all this stuff, I have an interface (as you can see on the picture) where I select the port to listen on and the amount of sockets, so instead of doing

PathSocket ps(SOCKET_TYPE_SERVER, 32);

I first fill ServerInfo struct (port & sockets) with needed information and will do

1
2
3
4
5
//Global initialization
PathSocket *ps;

//After getting needed information (port & sockets)
ps = new PathSocket(SOCKET_TYPE_SERVER, ServerInfo.sockets);
Last edited on
By chance, is the allocation method different between client and server ?
On the server side you make a heap allocation. is it the same client-side ?
If not, your problem could come from a badly initialized variable (for example a char buffer not initialized to zero).
the only difference is that I'm creating class instance using "new" with pointer in the server, but sorry, I did understood like 10% of what you said :/
If you write
1
2
PathSocket ps1(SOCKET_TYPE_SERVER, ServerInfo.sockets);
PathSocket *ps2 = new PathSocket(SOCKET_TYPE_SERVER, ServerInfo.sockets);

ps1 and *ps2 may not have the same content.

In your PathSocket::PacketSend() function, try replacing char cMsg[64]; with char cMsg[64] = {'\0'};
there's no cMsg array in PacketSend, PacketSend function just sends the packet via selected socket, cMsg is used when I press the button, anyway, doing the same thing for text buffer didnt help, HOWEVER this line in PacketReceive function did help

cTextBuffer[size] = '\0';

where size is return value of recv()
Resetting the content of cTextBuffer to 0 would have had the same effect.

If that really was the cause of the bug, you're lucky it didn't get worse.
resetting the content didnt do the job, removing everything after the message did :)
Topic archived. No new replies allowed.