Sign in to follow this  
Tim Lawton

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Edited by patrrr

Share this post


Link to post
Share on other sites

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;
	}
}
Edited by patrrr

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 =/

Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites

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