c - why select report read file descriptors are ready constantly? -


in order test select system call, wrote program receive data client:

#include <sys/types.h> #include <sys/socket.h> #include <sys/select.h> #include <sys/time.h> #include <unistd.h> #include <arpa/inet.h> #include <libgen.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <assert.h> #include <errno.h>  #define backlog 5  int main(int argc, char *argv[]) {     if (argc <= 2)     {         printf("usage: %s ip_address port_number\r\n", basename(argv[0]));         return 1;     }     const char *ip = argv[1];     int port = atoi(argv[2]);      int ret = 0;     struct sockaddr_in address;     memset(&address, 0, sizeof(address));     address.sin_family = af_inet;     inet_pton(af_inet, ip, &address.sin_addr);     address.sin_port = htons(port);      int listenfd = socket(af_inet, sock_stream, 0);     assert(listenfd != -1);     ret = bind(listenfd, (struct sockaddr *)&address, sizeof(address));     assert(ret != -1);     ret = listen(listenfd, backlog);     assert(ret != -1);      struct sockaddr_in client_address;     socklen_t client_addrlen = sizeof(client_address);     int connfd = accept(listenfd, (struct sockaddr *)&client_address, &client_addrlen);     if (connfd < 0)     {         printf("accept failed! errno %d\r\n", errno);         close(listenfd);     }     else     {         printf("accept success!\r\n");     }      char buf[1024];     fd_set read_fds;     fd_set exception_fds;     fd_zero(&read_fds);      fd_zero(&exception_fds);      while (1)     {         memset(buf, 0, sizeof(buf));         fd_zero(&read_fds);         fd_zero(&exception_fds);         fd_set(connfd, &read_fds);         fd_set(connfd, &exception_fds);         ret = select(connfd + 1, &read_fds, 0, &exception_fds, null);         if (ret < 0)         {             printf("selection failed!\r\n");             break;         }         if (fd_isset(connfd, &read_fds))         {             ret = recv(connfd, buf, sizeof(buf)-1, 0);             if (ret < 0)             {                 break;             }             printf("received %d bytes of normal data: %s\r\n", strlen(buf), buf);         }         else if (fd_isset(connfd, &exception_fds))         {             ret = recv(connfd, buf, sizeof(buf)-1, msg_oob);             if (ret < 0)             {                 break;             }             printf("received %d bytes of oob data: %s\r\n", strlen(buf), buf);         }     }      close(connfd);     close(listenfd);     return 0;  } 

however, when send data client program, server program above prints follows: accept success!

received 16 bytes of normal data: thisisnormaldata received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data:  received 0 bytes of normal data: 

(endless "received o bytes of normal data")

why select system call report read fds ready constantly?

select() reports fd ready because is ready. doesn't block (i presume).

consider implications of message keeps being printed, in light of excerpt documentation recv():

upon successful completion, recv() shall return length of message in bytes. if no messages available received , peer has performed orderly shutdown, recv() shall return 0.

since using stream socket, can indeed tell when other end has performed orderly shutdown. program telling that happened. willing tell many times ask. more typical response program make asking again, repeatedly, close local end of connection , move on other things (maybe such handling other connections).

possibly want select() listenfd file descriptor instead in order wait subsequent connections, though don't see you, don't @ point have other work performing blocking accept() problem.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -