演示结果:

示例代码:
//
// Created by linux on 2021/2/5.
//
#include <iostream>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
int main(int argc,char* argv[])
{
cout<<"linux select网络模型演示"<<endl;
//变量声明
int i=0;//客户端连接索引
int nready=0;
int maxi=-1;
unsigned short port = 8000;//服务监听端口
int maxListen = FD_SETSIZE;//客户端最大连接数量
//创建socket
int socket_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (socket_fd<0)
{
cout<<"创建sock
本文介绍了如何在Ubuntu 20.10中使用select函数进行网络编程,实现监听并处理来自多个客户端的并发连接和数据接收。通过示例代码展示了如何设置服务器监听端口,以及当客户端连接、断开时,服务器如何响应。测试结果显示,服务器能够成功处理5个客户端的同时连接,并且在客户端断开连接时,服务器能够捕获到这一变化。
订阅专栏 解锁全文
374

被折叠的 条评论
为什么被折叠?



