运行1个服务器和2个客户端
实现效果:
服务器和2个客户端互相聊天,服务器和客户端都需要使用select模型去实现
服务器要监视2个客户端是否连接,2个客户端是否发来消息以及服务器自己的标准输入流
客户端要监视服务器是否发来消息以及客户端自己的标准输入流 在不开线程的情况下,实现互相聊天
pollser.c
#include <myhead.h>
#define SER_PORT 6666 //服务器端口号
#define SER_IP "127.0.0.1" //服务器地址
int find_client(int *client_arr, int len, int client)//找到client所在位置
{
for(int i=0; i<len; i++)
{
if(client_arr[i] == client)
{
return i;
}
}
return -1;
}
void remove_client(int *client_arr, struct sockaddr_in *cin, int *len, int client)//删除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];
cin[i] = cin[i+1];
}
client_arr[i] = 0;
memset(cin+i, 0, sizeof(cin[i]));
(*len)--;
}
int main(int argc, char const *argv[])
{
//创建套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success\n");
//绑定服务器信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
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 readfds;
FD_ZERO(&readfds);
FD_SET(sfd, &readfds);//将套接字设置为监听状态
FD_SET(STDIN_FILENO, &readfds);//将服务器输入设置成监听状态
int client_arr[128] = {0}; // 客户端套接字文件描述符数组
int client_count = 0; // 链接的客户端的个数
struct sockaddr_in cin[128];//客户端信息
int size = sizeof(cin[0]);
while(1)
{
fd_set temp = readfds;
select(FD_SETSIZE, &temp, NULL, NULL, 0);//开始监听
// 服务器套接字文件描述符激活
if(FD_ISSET(sfd, &temp))
{
int client = accept(sfd, (struct sockaddr *)&cin[client_count], &size);
printf("[%s:%d] :已连接\n", inet_ntoa(cin[client_count].sin_addr), ntohs(cin[client_count].sin_port));
FD_SET(client, &readfds);//将新套接字加入监听
//监听列表变化
client_arr[client_count] = client;
client_count++;
}
// 标准输入流文件描述符激活
if(FD_ISSET(STDIN_FILENO, &temp))
{
char buf[128] = "";
int res = read(STDIN_FILENO, buf, sizeof(buf));
buf[strlen(buf)-1] = 0;
//将信息发送给所有人
for(int i=0; i<client_count; i++)
{
sendto(client_arr[i], buf, res, 0, (struct sockaddr *)&cin[i], size);
}
if(strcmp(buf, "quit") == 0)
break;
}
// 客户端套接字文件描述符激活
for(int i=0; i<client_count; i++)
{
int client = client_arr[i];
if(FD_ISSET(client, &temp))
{
char buf[128] = "";
struct sockaddr_in cin_temp;
int res = read(client, buf, sizeof(buf));
if(res == 0 || strcmp(buf, "quit") == 0)
{
printf("[%s:%d] :已断开\n", inet_ntoa(cin[i].sin_addr), ntohs(cin[i].sin_port));
FD_CLR(client, &readfds);
remove_client(client_arr, cin, &client_count, client);
close(client);
break;
}
printf("[%s:%d] :%s\n", inet_ntoa(cin[i].sin_addr), ntohs(cin[i].sin_port), buf);
}
}
}
close(sfd);
return 0;
}
pollcli.c
#include <myhead.h>
#define SER_PORT 6666
#define SER_IP "127.0.0.1"
int main(int argc, char const *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
// 定义一个监视列表
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(cfd, &readfds);
FD_SET(STDIN_FILENO, &readfds);
while(1)
{
fd_set temp = readfds;
select(FD_SETSIZE, &temp, NULL, NULL, 0);
// 标准输入流文件描述符激活
if(FD_ISSET(STDIN_FILENO, &temp))
{
char buf[128] = "";
int res = read(STDIN_FILENO, buf, sizeof(buf));
buf[strlen(buf)-1] = 0;
sendto(cfd, buf, res, 0, (struct sockaddr *)&sin, sizeof(sin));
if(strcmp(buf, "quit") == 0)
break;
}
// 客户端套接字文件描述符激活
if(FD_ISSET(cfd, &temp))
{
char buf[128] = "";
int res = read(cfd, buf, sizeof(buf));
printf("服务器:%s\n", buf);
if(strcmp(buf, "quit") == 0)
break;
}
}
close(cfd);
return 0;
}