[Study Note] select in Socket for Windows and Linux


Basic Concepts

I/O Model

how system handles I/O, which are send and recv

Sync/Async(for Client)

  • Sync: Do things one by one. When client submits request, it can do nothing before there is a result returned.
  • Async: Opposite to sync. The client can do other things, and there will be notification if receive result.
  • Difference: whether process is blocked when client accesses data

Block/Unblock(for Server)

  • Blocking: – The process waits until data coming into buffer. (like function recv)
  • Non-Blocking: Return immediately while the data in kernel isn’t ready. Process can do others. Once there is result, the server notifies client by select.
  • Difference: whether server returns immediately

I/O Models of Windows

  • Blocking Model
  • Select Model
  • WSAAsyncSelect Model
  • WSAEventSelect Model
  • Overlapped Model
  • Completion Port Model

Blocking model is just how default socket do. Easy to use, but we have to create multiple threads when there are several sockets. i.e. one connection occupies one thread.

This post will focus on select model.

This website shows threaded server and select server separately.

I/O Models of Linux

Blocking Non-Blocking
Synchronous Blocking Model Non-Blocking Model
Asynchronous I/O Multiplexing Model Asynchronous I/O Model
  • Blocking I/O
  • Non-Blocking I/O
  • I/O Multiplexing
  • Signal Driven I/O
  • Asynchronous I/O

Function select

select for Windows

Function Prototype

Parameters and Return Value Explanation

  • nfds: 0, ignored.
  • readfds: An optional pointer to a set of sockets to be checked for readability.
  • writefds: An optional pointer to a set of sockets to be checked for writability.
  • exceptfds: An optional pointer to a set of sockets to be checked for errors.
  • timeout: The maximum time for select to wait.NULL for blocking operation.
  • return value: The total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred.

note: select deletes socket without data under handling in fd_set


  • FD_CLR(s, *set)
    • Removes the descriptor s from set.
  • FD_ISSET(s, *set)
    • Nonzero if s is a member of the set. Otherwise, zero.
  • FD_SET(s, *set)
    • Adds descriptor s to set.
  • FD_ZERO(*set)
    • Initializes the set to the null set.

Steps for Usage

  1. Use macro FD_ZERO to initialize fd_set, which is the second, third or fourth parameter in select().
  2. Use macro FD_SET to add specific socket to corresponding fd_set. e.g. Add socket to readfds if we want to know if there are data to be received.
  3. Call select. If there are no data to be received, the corresponding socket will be removed from fd_set.
  4. Use macro FD_ISSET to check if a socket in fd_set.

select for Linux

Function Prototype

Parameters and Return Value Explanation

  • maxfd: the highest-numbered file descriptor in any of the three sets, plus 1
  • others: the same as Windows


  • FD_ZERO(int fd, fd_set* fds)
  • FD_SET(int fd, fd_set* fds)
  • FD_ISSET(int fd, fd_set* fds)
  • FD_CLR(int fd, fd_set* fds)

the same as Windows


Study Materials


One Reply to “[Study Note] select in Socket for Windows and Linux”

Leave a Reply

Your email address will not be published.