1 year ago

#373265

test-img

cs3cl4type

For a blocking UDP socket, select() returns (no error) with FD_ISSET(socket) being true, but subsequent recvmsg() blocks

A service-side UDP socket was created without the O_NONBLOCK flag, then in a while loop, the select() call returns (no error) and the socket fd is tested true from FD_ISSET. However, subsequently when I read from the socket using recvmsg(), the call blocks.

The simplified code is as follows:

int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sock;
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = <some IP>;
sock.sin_port = htons(<some port number>);
int rc = bind(fd, (struct sockaddr *)&sock, sizeof(sock));
// no error

while (1) {
   fd_set rset; // read
   FD_ZERO(&rset);
   FD_SET(fd, rset);
   rc = select(fd + 1, &rset, NULL, NULL, NULL); // no timeout
   if (rc <= 0) {
       // handles error or zero fd
       continue;
   }

   if (FD_ISSET(fd, rset)) {
       struct msghdr msg;
       // set up msg ...
       ret = recvmsg(fd, &msg, 0); // <------- blocks here
       // check ret
   }
}

What are some of the conditions that the UDP socket is readable but reading it would block?

c

sockets

network-programming

0 Answers

Your Answer

Accepted video resources