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

Started by
8 comments, last by Sik_the_hedgehog 11 years ago

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?

Advertisement

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.

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


private:

DWORD WINAPI RecvThread(LPVOID);

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;
	}
}

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.

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

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

Thanks man!

Hey man, that link 404'd on me

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.

This topic is closed to new replies.

Advertisement