1 Client Socket Programming (Review)![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/718c1d52e7cafa8653f80d4e2e615568.png)
2 Server Sockets
2.1 Binding a Socket: bind()
1.int bind(int socket, const struct sockaddr *address, socklen_t address_len);
2.2 Queuing incoming connections: listen()
int listen(int socket, int backlog);
- backlog是指listen等待的
connect
的个数,accept
会减少listen等待的个数,如果多于这个个数则拒绝
2.3 Accepting Incoming Connections: accept()
1.int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
2. accept返回一个文件描述符
3. server socket的文件描述符仍然存在,这样就可以接受更多的连接
4.accept
的文件描述符和server socket
的文件描述符不相同
5.accept
会开启新的端口,而server socket
则需要保证端口不变来接收新的连接
6.accept
是阻塞的,直到有新的连接进来
2.4 Communicating with the Client: read()/write()/close()
2.5 Putting it all together
3 Handling Multiple Incoming Connections of Server Sockets
3.1 Challenge of blocking
3.2 Identifying Readable File Descriptors with select()
参考:
https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/27/lec.html#coderef-bad_ref