标签:fail for void drs perror argc startup oid sea
1 #include<unistd.h> 2 #include<stdio.h> 3 #include<sys/select.h> 4 #include<sys/socket.h> 5 #include<netinet/in.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<assert.h> 9 10 #define SERVER_IP "127.0.0.1" 11 #define SERVER_PORT 9090 12 13 #define LISTEN_QUEUE_SIZE 5 14 15 void Perror(char *error) 16 { 17 perror(error); 18 exit(1); 19 } 20 21 int Startup(char *ip, short port) 22 { 23 int sockfd = socket(AF_INET, SOCK_STREAM, 0); 24 if(sockfd == -1) 25 Perror("socket"); 26 27 short ser_port = port; 28 char *ser_ip = ip; 29 30 struct sockaddr_in addrSer; 31 addrSer.sin_family = AF_INET; 32 addrSer.sin_port = htons(ser_port); 33 addrSer.sin_addr.s_addr = inet_addr(ser_ip); 34 35 36 int yes = 1; 37 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); 38 39 socklen_t len = sizeof(struct sockaddr); 40 int res = bind(sockfd, (struct sockaddr*)&addrSer, len); 41 if(res == -1) 42 Perror("bind"); 43 res = listen(sockfd, LISTEN_QUEUE_SIZE); 44 if(res == -1) 45 Perror("listen"); 46 47 return sockfd; 48 } 49 50 51 ye
1 #include"utili.h" 2 3 //select ip port 4 5 #define MAX_CLIENT_NUM 50000 6 7 void handler_msg(int sockConn) 8 { 9 char buffer[256]; 10 recv(sockConn, buffer, 256, 0); 11 printf("cli msg:> %s\n",buffer); 12 send(sockConn, buffer, strlen(buffer)+1, 0); 13 } 14 15 16 17 int main(int argc, char *argv[]) 18 { 19 assert(argc == 3); 20 short port = atoi(argv[2]); 21 int sockSer = Startup(argv[1], port); 22 23 int client_fd[MAX_CLIENT_NUM] = {0}; 24 fd_set readset; 25 26 int max_sock = sockSer; 27 int conn_num = 0; 28 int i; 29 while(1) 30 { 31 FD_ZERO(&readset); 32 FD_SET(sockSer, &readset); 33 for(i=0; i<MAX_CLIENT_NUM; ++i) 34 { 35 if(client_fd[i] != 0) 36 { 37 FD_SET(client_fd[i], &readset); 38 } 39 } 40 printf("select waiting......\n"); 41 int res = select(max_sock+1, &readset, NULL, NULL, NULL); 42 printf("select wakeup.......\n"); 43 if(res == -1) 44 { 45 perror("select"); 46 continue; 47 } 48 else if(res == 0) 49 { 50 printf("select Time Out.\n"); 51 continue; 52 } 53 else 54 { 55 //1 56 if(FD_ISSET(sockSer, &readset)) 57 { 58 struct sockaddr_in addrCli; 59 socklen_t len = sizeof(struct sockaddr); 60 int sockConn = accept(sockSer, (struct sockaddr*)&addrCli, &len); 61 if(sockConn == -1) 62 { 63 printf("Server Accept Client Connect Fail.\n"); 64 } 65 else 66 { 67 printf("Server Accept Client Connect OK.\n"); 68 client_fd[conn_num++] = sockConn; 69 70 if(sockConn > max_sock) 71 max_sock = sockConn; 72 } 73 continue; 74 } 75 76 for(i=0; i<conn_num; ++i) 77 { 78 if(FD_ISSET(client_fd[i], &readset)) 79 { 80 handler_msg(client_fd[i]); 81 } 82 } 83 } 84 85 } 86 87 return 0; 88 }
标签:fail for void drs perror argc startup oid sea
原文地址:http://www.cnblogs.com/51CC-YL/p/7360570.html