服务器:
#include <myhead.h>#define SER_PORT 6666
#define SER_IP "127.0.0.1"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, struct sockaddr_in *cin, 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];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;
}
客户端:
#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;
}
思维导图: