#include <myhead.h>void insert_client(int *client_arr,int *len,int client)
{//client_arr[n]={3,4} len=&client_count,client_count=2;//添加 5 client_arr[2(*len)]=5(client)client_arr[*len]=client;(*len)++;
}
int find_client(int *client_arr,int len,int client)
{for(int i=0;i<len;i++){if(client_arr[i]==client){return i;}}return -1;
}
void remove_client(int *client_arr,int *len,int client)
{int tar = find_client(client_arr,*len,client);if(tar==-1){return;}for(int i=tar;i<*len-1;i++){client_arr[i]=client_arr[i+1];}(*len)--;
}
int main(int argc, const char *argv[])
{
/* FILE* fp = popen("hostname -I","r");char ip[32] = {0};fread(ip,32,1,fp);printf("ip = %s\n",ip);*/if(argc!=2){printf("请输入正确的端口号\n");}fd_set readfds;int port = atoi(argv[1]);FD_ZERO(&readfds);int client_arr[100]={0};int client_count=0;int server =socket(AF_INET,SOCK_STREAM,0);if(server==-1){perror("socket");return -1;}printf("socket success\n");int reuse =1;if(setsockopt(server,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){perror("resue");return -1;}struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(port);sin.sin_addr.s_addr=inet_addr("192.168.2.97");if(bind(server,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("bind");return -1;}printf("bind success\n");if(listen(server,128)==-1){perror("listen");return -1;}FD_SET(server,&readfds);FD_SET(0,&readfds);// fd_set writefds;// FD_SET(0,&writefds);while(1){
/* fd_set arr=writefds;select(FD_SETSIZE,&arr,0,0,0);if(FD_ISSET(0,&arr)==1){char buf[128]="";scanf("%s",buf);while(getchar()!=10);for(int i=0;i<client_count;i++){int fd = client_arr[i];write(fd,buf,sizeof(buf));}printf("发送成功\n");}*/fd_set temp=readfds;select(FD_SETSIZE,&temp,0,0,0);if(FD_ISSET(server,&temp)){int client=accept(server,0,0);printf("有新客户端连接\n");FD_SET(client,&readfds);insert_client(client_arr,&client_count,client);}for(int i=0;i<client_count;i++){int client = client_arr[i];//就是每一个套接字if(FD_ISSET(client,&temp)==1){char buf[128]="";int res = read(client,buf,128);//客户端断开连接 read函数根据阻塞状态不同 返回值是不同的//read如果是一个非阻塞函数,客户端断开连接 read返回0//read如果是一个阻塞函数,客户端断开连接 read返回-1if(res==0){printf("有客户端断开连接\n");FD_CLR(client,&readfds);remove_client(client_arr,&client_count,client);close(client);break;}printf("客户端发来消息:%s\n",buf);}}if(FD_ISSET(0,&temp)==1){char buf[128]="";scanf("%s",buf);while(getchar()!=10);for(int i=0;i<client_count;i++){int fd = client_arr[i];write(fd,buf,sizeof(buf));}printf("发送成功\n");}}return 0;
}work2.c#include <myhead.h>
int main(int argc, const char *argv[])
{int sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd==-1){perror("sfd");return -1;}int reuse =1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){perror("sfd");return -1;}struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(6666);sin.sin_addr.s_addr=inet_addr("192.168.2.97");if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("find");return -1;}fd_set readfds;FD_ZERO(&readfds);FD_SET(sfd,&readfds);FD_SET(0,&readfds);while(1){fd_set temp = readfds;select(FD_SETSIZE,&temp,0,0,0);if(FD_ISSET(sfd,&temp)==1){char rbuf[128]="";read(sfd,rbuf,sizeof(rbuf));printf("数据为:%s\n",rbuf);}if(FD_ISSET(0,&temp)==1){char buf[128]="";scanf("%s",buf);while(getchar()!=10);int fd =sfd;write(fd,buf,sizeof(buf));printf("发送成功\n");}}close(sfd);return 0;
}