Select服务器
来源:互联网 发布:mac怎么解压缩文件 编辑:程序博客网 时间:2024/05/29 02:17
服务器代码:
#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>int array_fds[1024];//第三方数组,保存所有需要注意的I/Ostatic void Usage(const char *proc)//命令行输入错误打印错误信息{ printf("Usage: %s [local_ip] [local_port]\n",proc);}int startup(char *_ip, int _port)//创建套接字 并进行捆绑 监听{ int sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0) { perror("sock"); exit(2); } int flag = 1; setsockopt(sock, SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag)); struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(_port); local.sin_addr.s_addr = inet_addr(_ip); if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0) { perror("bind"); exit(3); } if(listen(sock,10)<0) { perror("listen"); exit(4); } return sock;}int main(int argc,char* argv[]){ if(argc != 3) { Usage(argv[0]); return 0; } int listenSock = startup(argv[1],atoi(argv[2])); int maxfd = 0; fd_set rfds;//此类型中每一个bit位对应一个文件描述符 int array_size = sizeof(array_fds)/sizeof(array_fds[0]); array_fds[0] = listenSock; int i = 1; for(;i < array_size;i++) { array_fds[i] = -1; } while(1){ struct timeval _timeout = {0,0}; FD_ZERO(&rfds);//将rfds中比特位清空 maxfd = -1; for(i = 0;i<array_size;i++){//从第三方数组中取出数据,设置rfds中对应比特位 if(array_fds[i] > 0){ FD_SET(array_fds[i],&rfds); if(array_fds[i] > maxfd){ maxfd = array_fds[i];//maxfd最大文件描述符 } } } switch(select(maxfd+1,&rfds,NULL,NULL,NULL/*&_timeout*/)){ case 0: printf("timeout...\n"); break; case -1: perror("select"); break; default: { int j = 0; for(;j < array_size;j++){ if(array_fds[j]<0){ continue; } if(j == 0&&FD_ISSET(array_fds[j],&rfds)){ struct sockaddr_in client; socklen_t len = sizeof(client); int new_fd = accept(array_fds[j],\ (struct sockaddr*)&client,&len); if(new_fd < 0){ perror("accept"); continue; } else{ printf("get a new client: (%s:%d)\n",\ inet_ntoa(client.sin_addr),\ ntohs(client.sin_port)); int k = 1; for(;k < array_size;k++){ if(array_fds[k] < 0){ array_fds[k] = new_fd; break; } } if(k == array_size){ close(new_fd); } } } else if(j != 0&&\ FD_ISSET(array_fds[j],&rfds)) { char buf[10240]; ssize_t s = read(array_fds[j],buf,sizeof(buf)-1); if(s>0) { buf[s] = 0; printf("client say: %s\n",buf); } else if(s == 0) { printf("client quit!\n"); close(array_fds[j]); array_fds[j] = -1; } else { perror("read"); close(array_fds[j]); array_fds[j] = -1; } } else { } } } break; } } return 0;}
客户端代码:
#include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> #include<sys/socket.h> #include<stdlib.h> #include<string.h> #include<strings.h> #include<sys/stat.h> #include<unistd.h> static void Usage(char * proc) { printf("Usage : %s [ip] [port]\n"); } int main(int argc, char* argv[]) { if(argc != 3) { Usage(argv[0]); return 1; } int sock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in peer; peer.sin_family = AF_INET; peer.sin_port = htons(atoi(argv[2])); peer.sin_addr.s_addr = inet_addr(argv[1]); if(connect(sock, (struct sockaddr*)&peer, sizeof(peer)) < 0) { perror("connect"); return 2; } char buf[10240]; //int sfd = dup(STDOUT_FILENO); while(1) { printf("Please Enter : "); fflush(stdout); ssize_t s = read(0, buf, sizeof(buf)-1); int sfd = dup(STDOUT_FILENO); if(s > 0) { buf[s-1] = 0; //write(sock, buf, strlen(buf)); //输出重定向 //close(1); // int new_fd = dup(sock); int new_fd = dup2(sock, 1); if(new_fd == -1) { perror("dup()"); return -1; } printf("%s",buf); fflush(stdout); //恢复stdout dup2(sfd, STDOUT_FILENO); ssize_t _s = read(sock, buf, sizeof(buf)-1); if(_s > 0) { buf[_s] = 0; printf("sever # %s \n", buf); } } } close(sock); return 0; }
阅读全文
0 0
- Select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- select服务器
- Select服务器
- select服务器
- select多路单线程服务器
- select模型服务器代码
- python 服务器 select 游戏
- tcp服务器(使用select)
- 34.Button控件(按钮,用于程序和用户的交互)
- Spring 上传文件
- SQL SERVER几种数据迁移/导出导入的实践
- angular4 项目启动报错
- mybatis教程
- Select服务器
- 由浅入深JavaScript6基础-语句1
- 王垠:自动编程是不可能的 我为什么不在乎人工智能
- 应用机器学习(九):主成分分析
- sql语句多条件查询
- java基本类型和引用类型
- 【转载】【Unity规则】【模型】Unity3D模型制作规范
- PCM音频数据音量大小调节
- mysql重连,连接丢失:The last packet successfully received from the server