Original Post
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:
[edited by - GeekPlusPlus on March 24, 2004 5:36:39 PM]
// 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++