Jump to content
  • Advertisement
Sign in to follow this  
Fixxer

UDP Help

This topic is 4345 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

Hi, I have just begun working on my networking class for my game, and here is what I have so far. UDP is new to me, and Sockets are not new, but I have not used them too much. Anyway, here is what I have so far in my networking header file, I am wondering if anyone could point what what I am doing wrong, and what I need to do.
class igeNetworking
{
public:
	igeNetworking();

	void ConnectToServer(char *sIPAddressx, char *sPlayerNamex);
	void SendPacket();
	void GetPacket();

private:

	SOCKET Socket;

	char *sIPAddress;
	char *sPlayerName;

};





igeNetworking::igeNetworking()
{
}




void igeNetworking::ConnectToServer(char *sIPAddressx, char *sPlayerNamex)
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,2), &wsaData);
	Socket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
	setsockopt(Socket,IPPROTO_UDP,SO_BROADCAST,NULL,NULL);

	sIPAddress = sIPAddressx;
	sPlayerName = sPlayerNamex;
}





void igeNetworking::SendPacket()
{
	sendto(Socket,"Testing 123...",strlen("Testing 123..."),NULL,(struct sockaddr*)sIPAddress,strlen(sIPAddress));
}





void igeNetworking::GetPacket()
{
	//recvfrom();
}

Share this post


Link to post
Share on other sites
Advertisement
You aren't checking error values... they will at least tell you what function is messing up.

You need to set up SOCKADDR_IN structures with the IP and port. Use inet_addr() to convert your const char* into a 4-byte ip. Check MSDN for more info on that stuff.

Share this post


Link to post
Share on other sites
First off, everything blaze02 said!

Second, are you sure you're putting everything in network byte order? Some systems don't require it, but it doesn't hurt to try (at least with my experience).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


SO_BROADCAST only works on the local subnet unless there is additinal hardwar to support it at the higher levels of network hierarchy


You might also want a DisconnectFromServer() to do cleanup.....

Share this post


Link to post
Share on other sites
I have updated connect to this:


//*****************************************************
// STARTS A UDP CONNECTION TO A SERVER
//*****************************************************
void igeNetworking::ConnectToServer(char *sIPAddressx, char *sPlayerNamex)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);

struct addrinfo *result = NULL, *ptr = NULL, hints;
int iResult;

ZeroMemory(&hints, sizeof(hints));

hints.ai_family = AF_UNSPEC;
hints.sockettype = SOCK_STREAM;
hints.prototype = IPPROTO_UDP;

iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);

Socket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
setsockopt(Socket,IPPROTO_UDP,SO_BROADCAST,NULL,NULL);

sIPAddress = sIPAddressx;
sPlayerName = sPlayerNamex;
}


but the struct addrinfo is causing problems.

Compiling...
main.cpp
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(48) : error C2079: 'hints' uses undefined struct 'addrinfo'
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(53) : error C2228: left of '.ai_family' must have class/struct/union type
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(54) : error C2228: left of '.sockettype' must have class/struct/union type
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(55) : error C2228: left of '.prototype' must have class/struct/union type
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(57) : error C2065: 'getaddrinfo' : undeclared identifier
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(57) : error C2065: 'argv' : undeclared identifier
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(57) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

InsaneCombat.exe - 7 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
Here's what MSDN has to say about addrinfo:


Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header Declared in Ws2tcpip.h.

Declared in Wspiapi.h on Windows 2000, Windows NT, and Windows Me/98/95.

Unicode Implemented as ADDRINFOW (Unicode) and ADDRINFOA (ANSI).


Pay special attention to the "declared in" part.

Share this post


Link to post
Share on other sites
Wait... I cant run a server using that UDP code unless its being run on windows server?

Share this post


Link to post
Share on other sites
No, that's not what the text says. Read it again. Especially this part: Header Declared in Ws2tcpip.h..

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!