![](https://i-blog.csdnimg.cn/direct/9ca980606b3f408991dda7c01debc5b7.png)
服务端
#include<head.h>
void insert_client(int* client_arr,int*len,int 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 ;}
int i=-1;
for(i=tar;i<*len-1;i++){
client_arr[i]=client_arr[i+1];
}
client_arr[i]=0;
(*len)--;
}
int main(int argc, char const *argv[])
{
if(argc!=2){
printf("请输入正确的端口号");
return 1;
}
int port=atoi(argv[1]);
fd_set readfds;
FD_ZERO(&readfds);
int client_arr[100]={0};
int client_count=0;
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd=%d\n",sfd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(port);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
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)){
int client=accept(sfd,0,0);
printf("客户端连接\n");
FD_SET(client,&readfds);
insert_client(client_arr,&client_count,client);
}
if(FD_ISSET(0,&temp)==1){
char buf[32]={0};
scanf("%s",buf);
while(getchar()!=10);
for(int i=0;i<client_count;i++){
int client=client_arr[i];
char arr[40]="服务端:";
strcat(arr,buf);
write(client,arr,32);
}
}
for(int i=0;i<client_count;i++){
int client=client_arr[i];
if(FD_ISSET(client,&temp)){
//客户端套接字激活有两种情况:一种是发来了消息,一种是断开连接
char buf[128]={0};
int res=read(client,buf,128);
//客户端断开连接,read函数根据阻塞状态不同,返回值是不同的
//read如果是一个阻塞性函数,客户端断开连接,read返回0
//read如果是一个非阻塞函数,客户端断开连接,read返回-1
if(res==0)
{
printf("有客户端断开了连接\n");
//客户端断开了连接 1.从监视列表readfds里面删除
FD_CLR(client,&readfds);
//2.从客户端数组中删除
remove_client(client_arr,&client_count,client);
//3.关闭套接字
close(client);
break;
}
if(i==0){
int client=client_arr[1];
char arr[40]="1号客户端:";
strcat(arr,buf);
write(client,arr,32);
printf("%d号客户端发来消息:%s\n",1,buf);
}else if(i==1)
{
int client=client_arr[0];
char arr[40]="2号客户端:";
strcat(arr,buf);
write(client,arr,32);
printf("%d号客户端发来消息:%s\n",2,buf);
}
// for(int i=0;i<client_count;i++){
// int client=client_arr[i];
// write(client,buf,32);
// }
}
}
}
return 0;
}
客户端
#include<head.h>
int main(int argc, char const *argv[])
{
if(argc!=2){
printf("请输入正确的端口号");
return 1;
}
int port=atoi(argv[1]);
fd_set readfds;
FD_ZERO(&readfds);
int client_arr[100]={0};
int client_count=0;
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd=%d\n",sfd);
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(port);
cin.sin_addr.s_addr=inet_addr("127.0.0.1");
struct sockaddr_in sin;
sin.sin_family = AF_INET; //通信域
sin.sin_port = htons(port); //服务器端口号
sin.sin_addr.s_addr = inet_addr("127.0.0.1"); //服务器ip地址
//3.2 连接服务器
if(connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("连接服务器成功\n");
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
while(1)
{
char buf[32]=" ";
fd_set temp=readfds;
select(FD_SETSIZE,&temp,0,0,0);
if(FD_ISSET(0,&temp)==1){
bzero(buf,32);
scanf("%s",buf);
while(getchar()!=10);
send(sfd,buf,32,0);
}
if(FD_ISSET(sfd,&temp)==1){
read(sfd,buf,32);
printf("%s\n",buf);
}
}
return 0;
}