linux 网络编程---->多客户请求服务器( C/S )实例—转
注意:可以开启一个server和多个client,同时可以对server进行发送data处理,请注意client的参数,格式是:./client IP char_data
IP:就是server所在的IP,char_data就是发送的数据
server.c
1 #include<sys/types.h> 2 #include<sys/socket.h> 3 #include<strings.h> 4 #include<arpa/inet.h> 5 #include<unistd.h> 6 #include<stdlib.h> 7 #include<stdio.h> 8 #include<string.h> 9 #include<errno.h> 10 #include<signal.h> 11 #include<sys/wait.h> 12 13 #define 14 LISTEN_PORT 6000 //!> 端口 15 #define MAX 5 //!> 最大的等待请求数目 16 17 void str_echo( int sockfd) //!> 从client中读取数据 18 { 19 ssize_t n; 20 charline[512]; 21 22 printf("准备读数据:"); 23 24 while( 1) 25 { 26 while( ( n =read( sockfd, line, 512 ) ) > 0) //!> 注意是:根据accept得到的client的操作描述符来处理的呗~ 27 { 28 line[n] ='\0'; 29 printf("Client send: %s\n", line ); 30 bzero(&line, sizeof( line )); //!> 再次置NULL 31 } 32 } 33 } 34 35 int main( int argc, char ** argv ) 36 { 37 int listenfd,connfd; //!> 描述符 38 pid_t childpid; //!> 子进程号 39 socklen_t chilen; //!> 40 41 structsockaddr_in chiaddr, servaddr; 42 43 //!> 建立socket( TCP ) 44 //!> 45 if( (listenfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) 46 { 47 printf("socket error... : %s\n",(char*)strerror(errno)); 48 exit(EXIT_FAILURE ); 49 } 50 51 //!> socket参数 52 //!> 53 servaddr.sin_family =AF_INET; //!> 协议族 54 servaddr.sin_port = htons( LISTEN_PORT); //!> IP 55 servaddr.sin_addr.s_addr =INADDR_ANY; //!> 本机IP 56 bzero(&( servaddr.sin_zero ), 8); //!> 8字节填充字符 57 58 //!> 绑定IP和端口 59 //!> 60 if( bind(listenfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 ) 61 { 62 printf("bind失败\n"); 63 exit(EXIT_FAILURE ); 64 } 65 66 //!> 下面开始监听 67 //!> 68 if( listen(listenfd, MAX ) == -1 ) 69 { 70 printf("listen error..."); 71 exit(EXIT_FAILURE ); 72 } 73 74 while( 1) 75 { 76 chilen =sizeof( chiaddr ); 77 78 //!> 等待连接 79 //!> 80 if( ( connfd= accept( listenfd, ( struct sockaddr* )&chiaddr,&chilen ) ) == -1 ) 81 { 82 printf("accept error...\n"); 83 exit(EXIT_FAILURE ); 84 } 85 else 86 { 87 printf("client连接成功!\n"); 88 } 89 90 //!> 创建子进程来处理请求 91 //!> 92 if( (childpid = fork() ) == 0 ) 93 { 94 close(listenfd ); //!> server关闭与其链接,交给子进程处理 95 printf("Client from %s\n", inet_ntoa( chiaddr.sin_addr )); 96 str_echo(connfd ); 97 exit(EXIT_SUCCESS ); 98 } 99 else if(childpid < 0 ) 100 { 101 printf("forkerror...\n"); 102 close(connfd ); 103 exit(EXIT_FAILURE ); 104 } 105 106 close(connfd ); 107 } 108 109 return0; 110 }
client.c
1 #include<sys/types.h> 2 #include<stdlib.h> 3 #include<stdio.h> 4 #include<unistd.h> 5 #include<sys/socket.h> 6 #include<strings.h> 7 #include<string.h> 8 #include<arpa/inet.h> 9 #include<errno.h> 10 11 #define SERVER_PORT 6000 12 13 void str_cli( char * data, int sockfd) //!> 此处是client发送消息而已 14 { 15 while( 1) 16 { 17 write(sockfd, data, strlen( data ) ); 18 sleep( 1); 19 } 20 21 exit( 0); 22 } 23 24 int main( int argc, char ** argv ) 25 { 26 intsockfd; 27 structsockaddr_in servaddr; 28 29 if( argc !=3 ) 30 { 31 printf("输入参数!\n"); 32 exit(EXIT_FAILURE ); 33 } 34 35 if( ( sockfd= socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) 36 { 37 printf("socket error...\n"); 38 exit(EXIT_FAILURE ); 39 } 40 41 bzero(&servaddr, sizeof( servaddr ) ); 42 servaddr.sin_family = AF_INET; 43 servaddr.sin_port = htons( SERVER_PORT ); 44 inet_pton(AF_INET, argv[1], &servaddr.sin_addr); //!> 需要自己写入IP 作为第一个参数 45 46 printf("Client connecting...\n"); 47 48 if( connect(sockfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 ) 49 { 50 printf("connect error..\n"); 51 exit(EXIT_FAILURE ); 52 } 53 54 printf("开始发送data:\n"); 55 str_cli(argv[2], sockfd ); 56 57 return0; 58 }
makefile:
1 all: server client 2 3 server: 4 gcc -W-o server server.c 5 6 7 client: 8 gcc -W -o client client.c 9 10 11 clean: 12 rm -f serverclient