本文主要是介绍linux网络通信之concurrency模型select,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
客户端
/*************************************************************************> File Name: client.c> Author: 联合作战实验室> Email: v.manstein@qq.com > Created Time: Fri 24 Mar 2017 07:57:28 AM PDT************************************************************************/#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>#define err_log(errlog) do {printf("%s:%s, %d\n", __FILE__, __func__, __LINE__);\perror(errlog); }while(0)
#define N 128int main(int argc, const char *argv[])
{int sockfd; int acceptfd;char buf[N] = {};struct sockaddr_in servervaddr; socklen_t addrlen = sizeof(struct sockaddr_in);if(argc < 3) {fprintf(stderr, "usage: %s serverip port.\n", argv[0]);return -1;}if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0) {err_log("socket error: ");}servervaddr.sin_family = AF_INET;servervaddr.sin_addr.s_addr = inet_addr(argv[1]);servervaddr.sin_port = htons(atoi(argv[2]));if(connect(sockfd, (struct sockaddr*)&servervaddr, sizeof(servervaddr)) < 0) {err_log("connect error: ");}printf("client connect server success.\n");while(1) {printf("Input: ");fgets(buf, N, stdin);buf[strlen(buf) - 1] = '\0';if(send(sockfd, buf, N, 0) < 0) {err_log("send error: ");}if(strncmp(buf, "quit", 4) == 0) {break;}if(recv(sockfd, buf, N, 0) <= 0) {err_log("recv error: ");}printf("From server: %s\n", buf);}close(sockfd);return 0;
}
服务器
/*************************************************************************> File Name: server.c> Author: 联合作战实验室> Email: v.manstein@qq.com > Created Time: Fri 24 Mar 2017 08:03:51 PM PDT************************************************************************/#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>#include<string.h>
#include<unistd.h>
#include<signal.h>#define err_log(errlog) do{printf("%s: %s, %d\n", __FILE__, __func__, __LINE__);\perror(errlog);} while(0)
#define N 128
#define LISTQUEUE 5int main(int argc, const char *argv[])
{int maxfd;int sockfd;int acceptfd;char buf[N] = {};fd_set readfds;fd_set tempfds;struct sockaddr_in serveraddr, clientaddr;socklen_t addrlen = sizeof(struct sockaddr_in);if(argc < 3) {fprintf(stderr, "usage: %s serverip port.\n", argv[0]);return -1;}if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {err_log("socket error: ");}serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) {err_log("bind error: ");}if(listen(sockfd, LISTQUEUE) < 0) {err_log("listen error: ");}// 初始化读文件描述符集合maxfd = sockfd;FD_ZERO(&readfds);FD_ZERO(&tempfds);FD_SET(sockfd, &readfds);printf("server init ok\n");while(1) {tempfds = readfds;// 监听描述符结合中的所有描述符,有满足条件的则清空// 描述符集合中未满足条件的描述符然后返回if(select(maxfd+1, &tempfds, NULL, NULL, NULL) < 0) {err_log("select error: ");}for(int i = 0; i < maxfd + 1; i++) {if(FD_ISSET(i, &tempfds)) {// 有新客户端请求链接if(i == sockfd) {if((acceptfd = accept(sockfd, (struct sockaddr*)&clientaddr, &addrlen)) < 0) {err_log("accept error: ");}printf("new connect form server: %d --> %s %d\n",acceptfd, inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); FD_SET(acceptfd, &readfds); // 添加到描述符集合中maxfd = maxfd > acceptfd ? maxfd : acceptfd;} else { // 检测到客户端数据有数据发送到来// pthread_create()if(recv(i, buf, N, 0) < 0) {err_log("recv error: ");}printf("From client: %s\n", buf);// 有客户端请求退出if(strncmp(buf, "quit", 4) == 0) {close(i); // 关闭该描述符FD_CLR(i, &readfds); // 从描述符监控集合中移除continue;}strcat(buf, "from server.");if(send(i, buf, N, 0) < 0) {err_log("send error: ");}}}}}close(sockfd);return 0;
}
这篇关于linux网络通信之concurrency模型select的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!