Sign in to follow this  

Asynchronous Sockets Not Responding

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my current project, I have a client/server Winsock architecture in which the server is non-blocking and the client is using Winsock Asynchronous Sockets. The client can send data as it should, and the server receives this data perfectly. (Testing on a LAN, haven't tested over the internet yet) There is a problem, however, when the server tries to send data back to the client. It seems as though the server is sending data ok, it at least gets past the sendto() function. Unfortunately, I'm getting no data on the client. Some Client Receive Code:
#define WM_NETMSG (WM_USER+5)

void CreateSocket(){

	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	if (s == INVALID_SOCKET){
		MessageBox(NULL, "Invalid Socket", "Error", MB_OK);
		PostQuitMessage(0);
	}

	WSAAsyncSelect(s, hwnd, WM_NETMSG, FD_READ);

	RecvAddr.sin_family = AF_INET;  //Server Address
	RecvAddr.sin_port = htons(8268);
	RecvAddr.sin_addr.s_addr = inet_addr("192.168.0.3");
}

void GetMessage(){

	char buffer[20];
	sockaddr_in sin;
	int recvaddrsize = sizeof(sin);

	recvfrom(s, buffer, sizeof(buffer), 0, (SOCKADDR*)&sin, &recvaddrsize);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		case WM_NETMSG:
			MessageBox(NULL, "Net Message", "Error", MB_OK);
			if (WSAGETSELECTERROR(lParam) == WSAENETDOWN){
				MessageBox(NULL, "NET DOWN", "CAPTION", MB_OK);
				return false;}
			else
				GetMessage();
		break;

...
...

		case WM_CREATE:
			if (InitWinsock())
				CreateSocket();
			inp.init(hwnd);
			if (kbd.init(hwnd))
				kbd.acquire();
			if (mous.init(hwnd))
				mous.acquire();
			else MessageBox(NULL, "FAILED INIT KEYBOARD", "ERROR", MB_OK);
			buffer = "cn";
			sendto(s, buffer, sizeof(buffer) + 1, 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
		break;

...
...


Some Server Send Code:

struct CLIENT
{
	unsigned short playerID;
	LPCSTR username;
	sockaddr_in client_addr;
	unsigned short xPos;
	unsigned short yPos;
	BYTE angle;
	unsigned short ammo;
	bool isDead;
};

...
...

void BroadcastMessage(LPCSTR message){

	for (int x = 0; x < players; x++){
		cout<<"SEND TO: "<<inet_ntoa(userList[x].client_addr.sin_addr)<<
				" PORT: "<<userList[x].client_addr.sin_port<<endl;
		sendto(s, (char*)message, sizeof(message) + 1, 0,
			  (SOCKADDR*)&userList[x].client_addr, sizeof(userList[x].client_addr));
		}
}

...
...


My client, presumably, isn't receiving any WM_NETMSG messages as I'm not getting any Message Boxes. On a separate note, when a client connects, I tried to set its port (for return purposes) to 13651. However, trying to output this right after setting it gives me 21301. Strangely, setting it to 21301 gives me 13651. It's not as big of a problem (or maybe it is), but still strange. Thanks Everybody, Jeff

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Port numbers should be specified in network byte order (ie big endian)

You should note that:
13651 = 0x3553
and
21301 = 0x5335

try htons() to (portably) convert to network-byte order (not that portability is likely to be an issue for you if you are using winsock, but it will at least work)

Share this post


Link to post
Share on other sites
Thanks AP, I was using htons() when i assigned the port, but not when i was outputting it. Unfortunately, I still haven't found the cause of my other problem.

EDIT: Typo

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
On closer inspection are you aware that you are only sending 5 bytes

(the size of the message POINTER + 1)

and are probably reading more than that, causing TCP to block until you recieve the rest of the buffer.

What you probably want is to either:

use constants for the buffer sizes on both ends,

or
always write the length of the buffer that you want to write first, and then the actual data.

At the client end you would then always read the size and then allocate a buffer of that size which you then read into.

Share this post


Link to post
Share on other sites

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this