Jump to content
  • Advertisement
Sign in to follow this  
Tim Lawton

Stumped! Why isn't my application sending packets?

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by Xooch

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Xooch

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!