UDP Help

Started by
9 comments, last by Fixxer 17 years, 9 months ago
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();
}

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.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
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).


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

For some reason when ever I try posting, it never works :-/

Click Here For Post
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)
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.
enum Bool { True, False, FileNotFound };
Wait... I cant run a server using that UDP code unless its being run on windows server?
No, that's not what the text says. Read it again. Especially this part: Header Declared in Ws2tcpip.h..
enum Bool { True, False, FileNotFound };
Oh ok, yea I got the header part.
Same errors.

This topic is closed to new replies.

Advertisement