2011年1月20日 星期四

Non-block Socket:select()

  • What 's socket
    • §Socket is a file descriptor associated with an opened file.
  • Blocking or non-blocking socket:
    • Blocking socket
      • The program is "blocked" until the request for data has been satisfied Socket default behavior
§
    • Non-blocking socket
      • Synchronous I/O Multiplexing
        • select() 




  • Select()
    • select() monitor readfds, writefds, and exceptfds.
    • fd: file descriptor
    • Use socket descriptor for file descriptor.
    • Provide the ability to monitor a set of sockets at the same time
      • Which socket is ready for read
      • Which socket is ready for write
    • Provide multiple sessions 
      • Several clients can access server at the same time
    • Non-blocking
      • Return to process if timer expires

  • Parameters in select()
int select(

int nfds, 

fd_set_t *readSet, 

fd_set_t *writeSet, 
fd_set_t *exSet, 
timeval_t *timeVal



  • nfds
    • Be the highest file descriptor plus one
  • readSet
    • Reflect which file descriptor your selected is ready for reading.
  • writeSet
    • Reflect which file descriptor your selected is ready for writing.
  • exSet
  • timeVal




  • Macro for select()
    • Bind() : FD_ZERO(), FD_SET()
    • Select(): FD_ISSET()
    • Close(): FD_CLR()
  • Sample code for select()


Reference:


沒有留言:

張貼留言