Stumped! Why isn't my application sending packets?

Started by
19 comments, last by rip-off 10 years, 11 months ago

Hey there,

I'm trying to create a server-client architecture application, which sends a struct called PLAYER which holds x,y,z as a float, the instance of PLAYER is Player.

Defined like so


struct PLAYER
{
float x, y, z;
};
PLAYER Player; 

Additional declarations in the header file


#define SERVER_ADDRESS	"127.0.0.1"
#define SERVER_PORT		17000

WSADATA Winsock;
SOCKET Socket;
sockaddr_in RemoteAddress;
int SizeInt;

These structs are available on both server and client applications, I then have this code to setup a socket on the

SERVER application:


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, (void*)this, 0, NULL);
}

Then a static function to receive packets from clients


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, &instance->SizeInt);

		instance->Player = Recv;
	}
}

That struct is then used like this to move the object


D3DXMatrixTranslation(&matTrans, Player.x, Player.y, Player.z);

CLIENT Application:

There is a similar init_winsock method:


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

	if(LOBYTE(Winsock.wVersion) !=2 || HIBYTE(Winsock.wVersion) !=2)	//Check version
	{
		WSACleanup();	//Cleanup and return false if version is wrong
		return;
	}

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

	//Input Receiver Information
	ZeroMemory(&RemoteAddress, sizeof(RemoteAddress));				//Clear Struct
	RemoteAddress.sin_family = AF_INET;								//Set the Address Family
	RemoteAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);		//Set IP Address
	RemoteAddress.sin_port = SERVER_PORT;							//Set Port
}

Then a function to send packets to the Server's Address


void Application::Send_Packet(PLAYER* data)
{
	sendto(Socket, (char*)data, sizeof(PLAYER), 0, (sockaddr*)&RemoteAddress, sizeof(sockaddr));
}

The DirectX11 object has initialization and rendered to the "Player.x/y/z" values in both applications.

Although the object does not appear on the Servers screen when a client connects and starts moving around, modifying the "Player.x/y/z" values with WASD keys, I can't figure out why

Advertisement

FYI, the post editor on this website eats your posts. Sorry bro :(

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Hey there,

I'm trying to create a server-client architecture application, which sends a struct called PLAYER which holds x,y,z as a float, the instance of PLAYER is Player.

Defined like so


struct PLAYER
{
float x, y, z;
};
PLAYER Player;

don't know if you not posting anything else is due to what Cornstalks said, but if it isn't.....we're ganna need a bit more....

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Sorry I hit post by accident, then when I went to do edit my internet cut out. Resulting in this confusing

what is your network setup? are you only sending to the loopback address, or are you trying to connect to another computer on your lan, or over the internet? have you checked firewalls?

you also arn't checking if bind has failed, or if you are getting any errors.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

I'm running both the applications on the local host, hence my SERVER_ADDRESS is 127.0.0.1

Fair point about the error checking, I will add that now and see what results I get.

EDIT: Definitely nothing wrong with the code, I've error checked it all. Either the Player.x/y/z isn't storing the values correct, I believe it is tho as I am able to edit their values with the movement keys so the PLAYER struct should update with it...

I will attempt to run one of my simpler networking application and see if it might be my firewall

UPDATE: My other networking applications seem to work fine, using almost the exact same code.

Can you post your updated code with the error checking? You are using threads, but not synchronisation, which is another problem.

This is the updated functions to check for errors:

CLIENT


bool Application::Init_Winsock()
{
	bool r;

	r = WSAStartup(MAKEWORD(2,2), &Winsock);
	if(!r)
	{return false;}

	if(LOBYTE(Winsock.wVersion) !=2 || HIBYTE(Winsock.wVersion) !=2)	//Check version
	{
		WSACleanup();	//Cleanup and return false if version is wrong
		return false;
	}

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

	//Input Receiver Information
	ZeroMemory(&RemoteAddress, sizeof(RemoteAddress));				//Clear Struct
	RemoteAddress.sin_family = AF_INET;								//Set the Address Family
	RemoteAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);		//Set IP Address
	RemoteAddress.sin_port = SERVER_PORT;							//Set Port
	
	return true;
}

bool Application::Send_Packet(PLAYER* data)
{
	bool r;

	r = sendto(Socket, (char*)data, sizeof(PLAYER), 0, (sockaddr*)&RemoteAddress, sizeof(sockaddr));
	if(!r)
	{return false;}

	return true;
}

The client is sending the updated version of the Player Struct every frame, using the update function be called by the Win32 class.



bool Application::Update()
{
	bool r;
	
	//Read user input
	r = m_Input->Update();
	if(!r)
	{return false;}

	//Chcek for Escape
	if(m_Input->IsEscapePressed() == true)
	{return false;}

	//Update System
	m_Timer->Update();

	//Update Input
	r = HandleInput(m_Timer->GetTime());
	if(!r)
	{return false;}

	//Update Networking
	Send_Packet(&Player);		//Send packets to server

	//Render Graphics
	r = RenderGraphics();
	if(!r)
	{return false;}

	return true;
}

SERVER



bool Application::Init_Winsock()
{
	bool r;

	r = WSAStartup(MAKEWORD(2,2), &Winsock);
	if(!r)
	{return false;}

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

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

	r = bind(Socket, (sockaddr*)&RemoteAddress, sizeof(RemoteAddress));
	if(!r)
	{return false;}

	//Start the recieve thread
	r = CreateThread(NULL, 0, RecvThread, (void*)this, 0, NULL);
	if(!r)
	{return false;}

	return true;
}

DWORD WINAPI Application::RecvThread(LPVOID knock)
{
	bool r;

	Application *instance = static_cast<Application *>(knock);


	while(true)
	{
		PLAYER Recv;

		r = recvfrom(instance->Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&instance->RemoteAddress, &instance->SizeInt);
		if(!r)
		{return false;}

		instance->Player = Recv;
	}
}
sendto() may return a value < 0 if it fails. Testing "not" on the return value is not good enough for error checking.

sin_port argument should be passed through htons(). However, it turns out that, because you don't do this in either sender or receiver, they will both get the same result :-)

recvfrom() doesn't seem to check errors at all. You should.
enum Bool { True, False, FileNotFound };

hmm, whats a better way of testing these functions then? Can you give me some sample code, if not too much trouble, please

This topic is closed to new replies.

Advertisement