Skip to main content
GameDev.net gamedev.net
🔒 Locked

Nonblocking UDP setting up the code?

Started by GeekPlusPlus Mar 24, 2004 at 4:34 PM 0 replies 500+ views
Original Post
GeekPlusPlus
GeekPlusPlus
Hello, I've been looking for a good long while, and reading the reams and reams of information that is all over this board about UDP and TCP and IOCP and all that great stuff, so don't start another one of those debates please, but all I'd like to know is how do I setup a non blocking UDP port? I have this:
// program init stuff here...

//

	// Create our UDP socket...

	if((iSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
		return 2;

	// Construct local address structure...

	memset(&ServerAddr, 0, sizeof(ServerAddr));
	ServerAddr.sin_family		= AF_INET;				// address family

	ServerAddr.sin_addr.s_addr	= htonl(INADDR_ANY);	// anyone can connect

	ServerAddr.sin_port			= htons(2222);			// setup the port


	// Bind to the socket...

	if(bind(iSock, (sockaddr*)&ServerAddr, sizeof(ServerAddr)) < 0)
		return 3;

	while(1)
	{
		iSizeOfClientAddr = sizeof(ClientAddr);
        // Block until receive message from a client?!?!?

        if((iMsgSize = recvfrom(iSock, Buffer, MAXBUFLEN, 0,
            (sockaddr*)&ClientAddr, &iSizeOfClientAddr)) < 0)
            return 4;

        printf("Handling client %s\n", inet_ntoa(ClientAddr.sin_addr));

         // Send received datagram back to the client

         if(sendto(iSock, Buffer, iMsgSize, 0, (sockaddr*) &ClientAddr, 
                     sizeof(ClientAddr)) != iMsgSize)
             return 5;
    }
//

// program ending stuff here...

//

But that's a blocking UDP server. I want to make this a non blocking so I can just dump stuff into my buffer as I get it for processing in the later part of the loop. Thanks EDIT: Format. - Newb Programmer: Geek++ [edited by - GeekPlusPlus on March 24, 2004 5:36:39 PM]
Newb Programmer: Geek++
Megahertz
Megahertz
I''m not sure how long you consider "a good long while" to be but I did a quick search on the multiplayer forum and found tons of threads with "non-blocking" in them. The 5th one down had the answer.

-=[ Megahertz ]=-
-=[Megahertz]=-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.