UDP Socket blocking

HiWe’re back again! We successfully managed to use the recv function in blocking mode for a TCP connection… we’re now experimenting with UDP, and we’re trying to implement a blocking function, using recvfrom… We assumed the same setsockopt which sets up the socket for blocking mode in TCP would work under UDP… but it doesn’t… can anyone help shed some light on this? Here’s a snippet of our code: /* set socket to use Blocking IO */ result = setsockopt(sock, SOL_SOCKET, SO_BIO, (char )&blockIO, sizeof(int)); blockIO = 0xff; … stuff deleted (binding etc) … do { szfsin = sizeof(struct sockaddr_in); / recvfrom blocks the first time we receive a byte, but subsequently doesn’t block!!! */ result = recvfrom(sock, rcvData, RCV_BUFFER_SIZE, 0, &fsin, &szfsin); } while (1); Thanks in advance.

In your example the blockIO variable is set after you call setsockopt. If this is true then it is not very strange that it is not blocking. I am using blocking UDP without problems. I still use bind though.

Yes, i do set it to 1 int blockIO = 1; as global variable on top of the everything.

Here is a part of my code. I don’t know if it helps but it works as expected anyway. --------------------------------------------------------- if ((SockID = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return; } //Enable broadcast and blocking Stat = setsockopt(SockID, SOL_SOCKET, SO_BROADCAST, char *)&Value, sizeof(int)); Stat = setsockopt(SockID, SOL_SOCKET, SO_BIO, (char *)&Value, sizeof(int)); memset((uc *)&SockAddr, 0, sizeof(SockAddr)); //Clear the structure SockAddr.sin_family = AF_INET; SockAddr.sin_addr.s_addr = htonl(INADDR_ANY); SockAddr.sin_port = htons(UDPDataPort); if (bind(SockID, &SockAddr, sizeof(SockAddr)) < 0) { socketclose(SockID); return; } SockAddrLength = sizeof(struct sockaddr_in); for (;:wink: { Count = recvfrom(SockID, UDPPacket, UDPPacketMaxSize, 0,&SockAddr, &SockAddrLength); … }