python - Can anyone help me to clear my understanding of TCP/IP System calls? -


  1. listen(5): here, mean server can connected 5 clients?
  2. select(): per wiki select system call used when continuously wants monitor port input/output,then if don't use select() can monitor particular port right?
  3. while accepting connection have use select() system call, why can't use simple array save newsfd?

for ex: (using python)

socketfd = socket.socket(socket.af_inet, socket.sock_stream) socketfd.bind((ip, port)) socketfd.listen(5) = 0 newsfd = [] while true:      newsfd[i] = socketfd.accept()      += 1      if == 5:          break 

  1. the argument listen backlog of connections - is, number of connections can queued kernel haven't accepted (because program can't respond instantly). set 1 if have 1 client, otherwise pass somaxconn.

  2. there 3 ways implement server: servers have 1 client @ time (and boring, though still have worry direction you're sending traffic), servers allocate 1 thread per client (which can costly , requires synchronization) , use blocking calls, , servers use single thread , use nonblocking calls (though possible load-balancing between multiple threads technique). focusing on third kind here.

in order use nonblocking sockets, must set o_nonblock flag (which allows read , write return eagain (in errno)), pass of sockets 1 of multiplexing syscalls. there select, poll better alternative , standard. nonstandard alternatives include epoll on linux , kqueue on *bsd (including mac os x).

your chosen multiplexing syscall let program sleep until there activity on 1 of sockets interested in. otherwise, have burn 100% cpu trying every file descriptor in turn though of them going eagain @ given moment, wasting cpu unforgivable sin. in (admittedly anecdotal) experience, server use 10% cpu typical number of clients (the limiting factor bandwidth of client). absolutely essential server uses 0% cpu when no client connected (unless has tasks must executed on timer when no client watching).

note multiplexing syscalls take timeout, should set how long until next timer scheduled (timers stored in heap , earliest timer root). if don't have timers scheduled, there special value indicates "forever".

  1. i'm not sure you're asking, don't have use select in order accept. however, if pass listening socket select (this required have more 1 client in nonblocking model), appear readable when accept might return new client (though spurious notifications always possible).

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -