- #include
-
- #define ERR_MSG(msg) \
- do \
- { \
- fprintf(stderr, "__%d__:", __LINE__); \
- perror(msg); \
- } while (0)
-
- #define IP "192.168.221.131"
- #define PORT 8080
-
- int keybord_event(fd_set readfds);
- int cliConnect_event(int, struct sockaddr_in *, fd_set *, int *);
- int cliRcvSnd_event(int, struct sockaddr_in *, fd_set *, int *);
-
- int main(int argc, const char *argv[])
- {
- //创建流失套接字
- int sfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
- printf("socket create success sfd = % d\n", sfd);
-
- //设置端口可以快速被复用
- int reuse = 1;
- if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
- {
- ERR_MSG("setsockopt");
- return -1;
- }
- printf("允许端口快速被复用成功\n");
-
- //填充地址信息给bind函数
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(PORT);
- sin.sin_addr.s_addr = inet_addr(IP);
-
- // bind 绑定服务器
- if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
- {
- ERR_MSG("bind");
- return -1;
- }
- printf("bind success\n");
-
- //将套接字设置为监听状态
- if (listen(sfd, 100) < 0)
- {
- ERR_MSG("linsten");
- return -1;
- }
- printf("listen success\n");
-
- //创建一个读合集
- fd_set readfds, tempfds;
- FD_ZERO(&readfds); //清空集合
- //将要检测的文件描述符添加到集合中
- FD_SET(0, &readfds);
- FD_SET(sfd, &readfds);
-
- int maxfd = sfd; //存储最大的文件描述符
- int s_res = -1;
- ssize_t res = -1;
- char buf[128] = "";
- struct sockaddr_in saveCin[1024]; //备份连接成功的客户端信息,用下标来对应文件描述符
-
- while (1)
- {
- tempfds = readfds;
- // select函数
- s_res = select(maxfd + 1, &tempfds, NULL, NULL, NULL);
- if (s_res < 0)
- {
- ERR_MSG("select");
- return -1;
- }
- else if (0 == s_res)
- {
- printf("time out\n");
- break;
- }
- for (int i = 0; i <= maxfd; i++)
- {
- if (FD_ISSET(i, &tempfds)==0)
- continue;
-
- if (0 == i) // 0为stdin,也就是有终端输入了
- {
- printf("键盘输入事件\n");
- keybord_event(readfds);
- }
- else if (sfd == i)
- {
- printf("客户端连接事件\n");
- cliConnect_event(sfd, saveCin, &readfds, &maxfd);
- }
- else
- {
- printf("客户端交互事件\n");
- cliRcvSnd_event(i, saveCin, &readfds, &maxfd);
- }
- }
- }
-
- if (close(sfd) < 0)
- {
- ERR_MSG("close");
- return -1;
- }
-
- return 0;
- }
-
- //键盘输入事件的函数体
- int keybord_event(fd_set readfds)
- {
- char buf[128] = "";
- int sndfd = -1;
- memset(buf, 0, sizeof(buf));
- int res = scanf("%d %s", &sndfd, buf);
- while (getchar() != 10)
- ;
- if (res != 2)
- {
- printf("输入的数据格式有误 :fd string\n");
- return -1;
- }
- if (sndfd <= 2 || FD_ISSET(sndfd, &readfds) == 0)
- {
- printf("非法的文件描述符\n");
- return -1;
- }
- // 将文件描述符设置为非阻塞模式
- int flags = fcntl(sndfd, F_GETFL, 0);
- flags |= O_NONBLOCK;
- if (fcntl(sndfd, F_SETFL, flags) == -1)
- {
- ERR_MSG("fcntl");
- return -1;
- }
- if (send(sndfd, buf, sizeof(buf), 0) < 0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("send success\n");
- return 0;
- }
-
- //客户端连接事件的函数体
- int cliConnect_event(int sfd, struct sockaddr_in saveCin[], fd_set *preadfds, int *pmaxfd)
- {
- int newfd = -1;
- struct sockaddr_in cin; //存储客户端的地址信息
- memset(&cin, 0, sizeof(cin));
- socklen_t addrlen = sizeof(cin); //真实的地址信息结构体的大小
- newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
- if (newfd < 0)
- {
- ERR_MSG("accept");
- return -1;
- }
- printf("[%s:%d]客户端连接成功 newfd = %d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
- saveCin[newfd] = cin; //将cin另存到newfd对应的下标
- FD_SET(newfd, preadfds); //将newfd添加到集合
- *pmaxfd = *pmaxfd > newfd ? *pmaxfd : newfd; //更新maxfd
-
- return 0;
- }
-
- //客户端交互事件的函数体
- int cliRcvSnd_event(int fd, struct sockaddr_in *saveCin, fd_set *preadfds, int *pmaxfd)
- {
- char buf[128] = "";
- memset(buf, 0, sizeof(buf));
- //接收数据
- ssize_t res = recv(fd, buf, sizeof(buf), 0);
- if (res < 0)
- {
- ERR_MSG("recv");
- return -1;
- }
- else if (0 == res)
- {
- printf("[%s:%d]客户端连接关闭 newfd = %d\n", inet_ntoa(saveCin[fd].sin_addr), ntohs(saveCin[fd].sin_port), fd);
- close(fd);
- FD_CLR(fd, preadfds);
- while (FD_ISSET(*pmaxfd, preadfds) == 0 && (*pmaxfd)-- >= 0)
- ;
- return 0;
- }
- printf("[%s:%d] newfd=%d : %s\n", inet_ntoa(saveCin[fd].sin_addr), ntohs(saveCin[fd].sin_port), fd, buf);
- //发送数据
- strcat(buf, "已收到");
- if (send(fd, buf, sizeof(buf), 0) < 0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("send success\n");
- return 0;
- }