[笔记] about network - xieyunzi/xieyunzi.github.io GitHub Wiki
The structure of a non-blocking tcp server
The system calls above are only explained superficially so that grasping the details will become easier. All the details are covered in the links in this post.
The non-blocking tcp server can be implemented as follows:
- Create a socket
- Mark it as non-blocking (this will make even the accept call non-blocking)
- Bind it to an address
- Listen on the socket
- Create an epoll instance
- Add your socket to the epoll instance (this way the incoming requests can be monitored through event notification)
- Create a read event queue
- Create threads for processing tasks from read queue
- Create a write event queue
- Create threads for processing tasks in the write queue
- Wait for events on epoll instance in a loop
- For incoming requests events
- call accept
- mark the accepted socket as non-blocking
- add it to the epoll instance for monitoring
- For read events, push the file descriptor and user data to read event queue
- For write events, push the file descriptor and user data to write event queue
- For close events, remove the file descriptor from the epoll instance
https://techtake.info/2016/09/19/non-blocking-tcp-server-explained/
本机开放的端口
# linux
netstat -tanp | grep LISTEN
# mac
netstat -tan | grep LISTEN