Archived

This topic is now archived and is closed to further replies.

Nonblocking UDP setting up the code?

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

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]

Share this post


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

Share this post


Link to post
Share on other sites