ser.c
#include <myhead.h>
#define SER_IP "10.168.1.111" //服务器IP
#define SER_PORT 8888 //服务器端口号
struct User
{char username[20];struct sockaddr_in cin;
};
int main(int argc, const char *argv[])
{struct User ur[1024];//1、创建用于监听的套接字int sfd = -1;sfd = socket(AF_INET, SOCK_STREAM, 0);if (sfd == -1){perror("socket error");return -1;}printf("sfd = %d\n", sfd);//将端口号快速重用int reuse = 1;if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");//2、绑定ip地址和端口号//2.1填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //IP地址//2.2绑定if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、启动监听,允许客户端连接if (listen(sfd, 128) == -1){perror("listen error");return -1;}printf("listen success\n");struct sockaddr_in cin; //用于接收客户端地址信息结构体socklen_t socklen = sizeof(cin); //用于接收客户端地址信息的大小int newfd = -1; //新创建用于通信的套接字文件描述符char sbuf[128] = ""; //从键盘上输入数据//1、创建文件描述符容器fd_set readfds, tempfds;//2、清空容器内容FD_ZERO(&readfds);//3、将sfd和0号文件描述符放入容器中FD_SET(0, &readfds);FD_SET(sfd, &readfds);int maxfd = sfd; //存放容器中最大的文件描述符的值struct sockaddr_in cin_arr[1024]; //存放客户端对应的地址信息while (1){tempfds = readfds; //将要检测的容器复制保存一份int res = select(maxfd + 1, &tempfds, NULL, NULL, NULL); //阻塞等待集合中事件产生if (res == -1){perror("select error");return -1;}else if (res == 0){printf("time out\n");return -1;}//遍历所有的容器中的文件描述符for (int i = 0; i <= maxfd; i++){//判断当前的文件描述符是否在tempfds中,如果不在,直接下一个if (!FD_ISSET(i, &tempfds)){continue;}//如果程序执行到此,说明检测的集合中的i文件描述符产生了事件if (i == sfd) //表示sfd触发了事件{newfd = accept(sfd, (struct sockaddr *)&cin, &socklen);if (newfd == -1){perror("accept error");return -1;}recv(newfd, ur[newfd].username, sizeof(ur[newfd].username), 0);printf("%s [%s:%d]登陆成功\n", ur[newfd].username, inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));//将客户端文件描述符放入到集合中FD_SET(newfd, &readfds);cin_arr[newfd] = cin; //将新的客户端地址信息放入容器//更新一下maxfdif (newfd > maxfd){maxfd = newfd;}bzero(sbuf, sizeof(sbuf));sprintf(sbuf, "-----%s登陆成功-----", ur[newfd].username);//将消息发给每一个客户端for (int k = 4; k <= maxfd; k++){if (k == i)continue;send(k, sbuf, strlen(sbuf), 0);}}else if (i == 0) //表示有终端输入事件{bzero(sbuf, sizeof(sbuf));//从键盘输入数据fgets(sbuf, sizeof(sbuf), stdin);sbuf[strlen(sbuf) - 1] = 0;char buf[128] = "**system**:";strcat(buf, sbuf);for (int k = 4; k <= maxfd; k++){send(k, buf, strlen(buf), 0);}}else{//5、通信套接字与客户端进行数据收发char rbuf[128] = "";//清空容器bzero(rbuf, sizeof(rbuf));//从套接字中读取数据int res = recv(i, rbuf, sizeof(rbuf), 0);if (res == 0){sprintf(rbuf, "-----%s已下线-----", ur[i].username);for (int k = 4; k <= maxfd; k++){if (k == i)continue;send(k, rbuf, strlen(rbuf), 0);}bzero(ur[i].username, sizeof(ur[i].username));//6、关闭套接字close(i);//将当前文件描述符从容器中移除FD_CLR(i, &readfds);//更新maxfdfor (int j = maxfd; j >= 0; j--){if (FD_ISSET(j, &readfds)){maxfd = j;break;}}}else if (strcmp(rbuf, "quit") != 0){printf("%s [%s:%d]chat成功\n", ur[i].username, inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));char wbuf[128] = "";strcat(wbuf, ur[i].username);strcat(wbuf, ":");strcat(wbuf, rbuf);// 将消息发给每一个客户端for (int k = 4; k <= maxfd; k++){if (k == i)continue;send(k, wbuf, strlen(wbuf), 0);}}bzero(rbuf,sizeof(rbuf));}}}close(sfd);return 0;
}
cli.c
#include <myhead.h>
#define SER_IP "10.168.1.111" //服务器ip
#define SER_PORT 8888 //服务器端口号
struct Msg
{// char type; //log char quitchar username[20];char msgText[1024];
};
int main(int argc, const char *argv[])
{struct Msg ur = {0, 0};//1、创建用于通信的套接字文件描述符int cfd = socket(AF_INET, SOCK_STREAM, 0);if (cfd == -1){perror("socket error");return -1;}//2、绑定(非必须)//3、连接服务器//3.1填充要连接的服务器地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //ip地址printf("请输入姓名>>>");scanf("%s", ur.username);getchar();//3.2 连接服务器if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("connect success\n");//登入信息send(cfd, ur.username, strlen(ur.username), 0);struct pollfd pfd[2];pfd[0].fd = 0;pfd[0].events = POLLIN;pfd[1].fd = cfd;pfd[1].events = POLLIN;//4、数据收发// char wbuf[128] = "";while (1){int res = poll(pfd, 2, -1);if (res == -1){perror("poll error");return -1;}else if (res == 0){printf("time out\n");return -1;}if (pfd[0].revents == POLLIN){//清空数据bzero(&ur.msgText, sizeof(ur.msgText));// printf("请输入>>>");fgets(ur.msgText, sizeof(ur.msgText), stdin); //从终端输入ur.msgText[strlen(ur.msgText) - 1] = 0;//将数据发送给服务器send(cfd, ur.msgText, strlen(ur.msgText), 0);// printf("发送成功\n");//判断发送的数据if (strcmp(ur.msgText, "quit") == 0){break;}}if (pfd[1].revents == POLLIN){//接收服务器发来的消息//清空数据bzero(ur.msgText, sizeof(ur.msgText));recv(cfd, ur.msgText, sizeof(ur.msgText), 0);printf("%s\n", ur.msgText);}}//5、关闭套接字close(cfd);return 0;
}
演示视频:
网络编程小项目