Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 11 April 2013 - 07:04 AM

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?



Sponsor:

#2 patrrr   Members   -  Reputation: 1005

Like
0Likes
Like

Posted 11 April 2013 - 07:26 AM

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, 11 April 2013 - 07:28 AM.


#3 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 11 April 2013 - 07:37 AM

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

 

private:

DWORD WINAPI RecvThread(LPVOID);


#4 patrrr   Members   -  Reputation: 1005

Like
1Likes
Like

Posted 11 April 2013 - 07:45 AM

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, 11 April 2013 - 07:45 AM.


#5 hikrea   Members   -  Reputation: 161

Like
0Likes
Like

Posted 11 April 2013 - 07:48 AM

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.



#6 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 11 April 2013 - 08:03 AM

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



#7 hikrea   Members   -  Reputation: 161

Like
0Likes
Like

Posted 11 April 2013 - 08:34 AM

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



#8 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 11 April 2013 - 08:53 AM

Thanks man!



#9 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 11 April 2013 - 09:35 AM

Hey man, that link 404'd on me



#10 Sik_the_hedgehog   Crossbones+   -  Reputation: 1747

Like
0Likes
Like

Posted 11 April 2013 - 11:35 PM

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, 11 April 2013 - 11:36 PM.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS