Skip to main content
GameDev.net gamedev.net
🔒 Locked

First time attempting multi-player with UDP sockets, weird error

Started by Tim Lawton Apr 11, 2013 at 1:04 PM 8 replies 2k views
Original Post
Tim Lawton
Tim Lawton

Hey guys,

Pretty much all the details in the title, I've no errors from the client side, but this is the server side code:

SERVER


void Application::Init_Winsock()
{
	WSAStartup(MAKEWORD(2,2), &Winsock);

	if(LOBYTE(Winsock.wVersion) !=2 || HIBYTE(Winsock.wVersion) !=2)	//Check Version
	{
		WSACleanup();	//Cleanup and end if not version 2
		return;
	}

	//Create Socket
	Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	//Input Reciever Information
	ZeroMemory(&RemoteAddress, sizeof(RemoteAddress));		//Clear Struct
	RemoteAddress.sin_family = AF_INET;						//Set the Family Address
	RemoteAddress.sin_port = SERVER_PORT;					//Set Port
	bind(Socket, (sockaddr*)&RemoteAddress, sizeof(RemoteAddress));

	//Start the recieve thread
	CreateThread(NULL, 0, RecvThread, NULL, 0, NULL);
}

DWORD WINAPI Application::RecvThread(LPVOID knock)
{
	while(true)
	{
		PLAYER Recv;
		recvfrom(Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&RemoteAddress, &SizeInt);

		Player = Recv;
	}
}

And heres the error:

networkerror.png

Im clueless guys, from the tutorial Im using this should work?

patrrr
patrrr

Is the RecvThread function static? Otherwise, it also expects a 'this' pointer to work on, which is usually what you pass in the first parameter ('knock') in these cases.

Tim Lawton
Tim Lawton

I believe so, its declared in the header file like so:


private:

DWORD WINAPI RecvThread(LPVOID);
patrrr
patrrr

Then it's not static, it would look like this if it was:


static DWORD WINAPI RecvThread(LPVOID);

I can see you're also using class instance variables in RecvThread; "Socket", "RemoteAddress", etc. They're not accessible due to the function being static.

You can use "knock" as the object instance, like this:


DWORD WINAPI Application::RecvThread(LPVOID knock)
{
	Application *instance = static_cast<Application *>(knock);
	while(true)
	{
		PLAYER Recv;
		recvfrom(instance->Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&instance->RemoteAddress, &SizeInt);

		Player = Recv;
	}
}
Andrey Buhtojarov
Andrey Buhtojarov


Then it's not static, it would look like this if it was:


static DWORD WINAPI RecvThread(LPVOID);

I can see you're also using class instance variables in RecvThread; "Socket", "RemoteAddress", etc. They're not accessible due to the function being static.

You can use "knock" as the object instance, like this:


DWORD WINAPI Application::RecvThread(LPVOID knock)
{
	Application *instance = static_cast<Application *>(knock);
	while(true)
	{
		PLAYER Recv;
		recvfrom(instance->Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&instance->RemoteAddress, &SizeInt);

		Player = Recv;
	}
}

Pass this pointer like arg into the thread. In the thread cast PVOID to you object pointer and use it.

Tim Lawton
Tim Lawton

Sorry guys, I don't understand what you mean. I'm not very good with understanding the correct terms of things

Andrey Buhtojarov
Andrey Buhtojarov

Sorry guys, I don't understand what you mean. I'm not very good with understanding the correct terms of things

Declare as static:


private:

static DWORD WINAPI RecvThread(LPVOID);
 

Pass instance pointer as thead parametr:


...
    RemoteAddress.sin_port = SERVER_PORT;                    //Set Port
    bind(Socket, (sockaddr*)&RemoteAddress, sizeof(RemoteAddress));

    //Start the recieve thread
    CreateThread(NULL, 0, RecvThread, (void *)this, 0, NULL);
}
 


Then it's not static, it would look like this if it was:


static DWORD WINAPI RecvThread(LPVOID);

I can see you're also using class instance variables in RecvThread; "Socket", "RemoteAddress", etc. They're not accessible due to the function being static.

You can use "knock" as the object instance, like this:


DWORD WINAPI Application::RecvThread(LPVOID knock)
{
	Application *instance = static_cast<Application *>(knock);
	while(true)
	{
		PLAYER Recv;
		recvfrom(instance->Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&instance->RemoteAddress, &SizeInt);

		Player = Recv;
	}
}

And you must correctly finish the thread. You can use this http://msdn.microsoft.com/ru-RU/library/windows/desktop/ms682396(v=vs.85).aspxhttp://msdn.microsoft.com/ru-RU/library/windows/desktop/ms682396(v=vs.85).aspx

Tim Lawton
Tim Lawton

Thanks man!

Tim Lawton
Tim Lawton

Hey man, that link 404'd on me

Sik_the_hedgehog
Sik_the_hedgehog

Because the URL is repeated in the link =S This is the correct one:

http://msdn.microsoft.com/ru-RU/library/windows/desktop /ms682396(v=vs.85).aspx

...that's in Russian though, try this instead (in English):

http://msdn.microsoft.com/en-US/library/windows/desk top/ms682396(v=vs.8 5).aspx

EDIT: fixed, turns out the forum can't understand links with parenthesis in them =/

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.