1

In my program, there is a thread blocking on a listen socket, which waits for other connections, and the code likes this:

{
    ......
    FD_ZERO(&fd_sets);
    FD_SET(sock_fd, &fd_sets);

    ret_val = select(sock_fd + 1, &fd_sets, NULL, NULL, NULL);

    if (ret_val > 0)
    {
         accept(sock_fd, NULL, NULL);
         ......
    }
    else
    {
        ......
    }

Per my understandings, if in other thread, shutdown the socket, and the code likes this:

{
    ......
    shutdown(sock_fd, SHUT_RD);
    ......
}  

I think the select() in the previous thread should return. But after testing, I find the select() is still in blocking.

Why shutdown a socket can't let the select() return?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • 2
    You might want to [read this](http://lists.gnu.org/archive/html/libmicrohttpd/2011-09/msg00026.html). It seems that Solaris doesn't unblock when you `shutdown` a listening socket. – Some programmer dude Aug 21 '13 at 08:13
  • @JoachimPileborg: Yes, after check the return error code of shutdown, it is ENOTCONN (/* Socket is not connected */), so I think I should consider other methods to fix this issue. Thanks! – Nan Xiao Aug 21 '13 at 08:29
  • 1
    You could consider a bit of error handling in your code, instead of just assuming everything works, and throwing away the result of accept(). If you'd done that you wouldn't be asking this question. – user207421 Aug 21 '13 at 09:49
  • @EJP: OK, thanks for your advice! This code is merely a demo, and I will add error handling in production code. – Nan Xiao Aug 21 '13 at 10:00
  • 1
    No. Add it now. It's there for a purpose. Adding it in production is already too late, as this question proves. You're wasting your own time by deferring this. – user207421 Aug 21 '13 at 10:18
  • @EJP: Yes, thanks for your sincere advice! – Nan Xiao Aug 22 '13 at 01:36

0 Answers0