creating a non-blocking socket?

Started by
1 comment, last by The Beholder 17 years, 12 months ago
Hmm... I'm feeling a bit stupid for asking this, but after searching this forum, the MSDN WinSock reference and google for a while now I give up. In every text I've read on sockets, non-blocking sockets are mentioned. What's not mentioned is how to make a socket non-blocking. How is this done? I've created a socket like so: SOCKET s = socket(AF_INET, SOCK_DGRAM, 0); And then I receive data like this: sockaddr_in saServer; saServer.sin_family = AF_INET; saServer.sin_addr.s_addr = htonl(INADDR_ANY); saServer.sin_port = htons(27017); bind(s, (sockaddr*) &saServer, sizeof(saServer)); sockaddr_in saClient; int clientSize = sizeof(saClient); char buf[1024]; memset(buf, 0, sizeof(buf)); recvfrom(s, buf, sizeof(buf), 0, (sockaddr*) &saClient, &clientSize); I read that the last parameter is optional but the communication doesn't work at all when I replace it with 0.
Advertisement
You use ioctlsocket:
u_long ulNonblocking = 1;ioctlsocket(s, FIONBIO, &ulNonblocking);


EDIT: Nonblocking sockets usually refers to using select to poll a socket or wait for data to be ready to be read:
FD_SET fd;timeval timeVal;FD_ZERO(&fd);FD_SET(s, &fd)timeVal.tv_sec = 0;timeVal.tv_usec = 50000; // 50msint nRet = select(s, &fd, NULL, NULL, &timeVal);if(nRet == 0){   // Nothing to read}else if(nRet == -1){   // Error}else{   // socket can be read from}
thanks alot!

This topic is closed to new replies.

Advertisement