码迷,mamicode.com
首页 > 其他好文 > 详细

socket学习笔记——并发服务器与I/O程序分割客户端

时间:2015-08-17 15:20:28      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

client.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <arpa/inet.h>
 6 #include <sys/socket.h>
 7 
 8 #define BUF_SIZE 30
 9 void error_handling(char* message);
10 void read_routine(int sock,char* buf);
11 void write_routine(int sock,char* buf);
12 
13 int main(int argc,char* argv[])
14 {
15     int sock;
16     pid_t pid;
17     char buf[BUF_SIZE];
18     struct sockaddr_in serv_addr;
19     if(argc != 3)
20     {
21         printf("usage: %s <ip><port>\n",argv[0]);
22         exit(1);
23     }
24 
25     sock = socket(PF_INET,SOCK_STREAM,0);
26     memset(&serv_addr,0,sizeof(serv_addr));
27     serv_addr.sin_family = AF_INET;
28     serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
29     serv_addr.sin_port = htons(atoi(argv[2]));
30 
31     if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
32         error_handling("connect error");
33 
34     pid = fork();
35     if(pid == 0)
36         write_routine(sock,buf);
37     else
38         read_routine(sock,buf);
39 
40     close(sock);
41     return 0;
42 }
43 
44 void read_routine(int sock,char* buf)
45 {
46     while(1)
47     {
48         int str_len;
49         str_len = read(sock,buf,BUF_SIZE);
50         if(str_len == 0)
51             return;
52         buf[str_len] = 0;
53         printf("message from server;%s",buf);
54     }
55 }
56 
57 void write_routine(int sock,char* buf)
58 {
59     while(1)
60     {
61         fgets(buf,BUF_SIZE,stdin);
62         if(!strcmp(buf,"q\n") || !strcmp(buf,"Q\n"))
63         {
64             shutdown(sock,SHUT_WR);
65             return;
66         }
67         write(sock,buf,strlen(buf));
68     }
69 }
70 
71 void error_handling(char* message)
72 {
73     fputs(message,stderr);
74     fputc(\n,stderr);
75     exit(1);
76 }

server.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <signal.h>
 6 #include <sys/wait.h>
 7 #include <arpa/inet.h>
 8 #include <sys/socket.h>
 9 
10 #define BUF_SIZE 30
11 void error_handling(char* message);
12 void read_childproc(int sig);
13 
14 int main(int argc,char* argv[])
15 {
16     int serv_sock,clnt_sock;
17     struct sockaddr_in serv_addr,clnt_addr;
18     struct sigaction act;
19     pid_t pid;
20     socklen_t adr_sz;
21     int str_len ,state;
22     char buf[BUF_SIZE];
23 
24     if(argc != 2)
25     {
26         printf("usage:%s <port>\n",argv[0]);
27         exit(1);
28     }
29 
30     act.sa_handler = read_childproc;
31     sigemptyset(&act.sa_mask);
32     act.sa_flags = 0;
33     state = sigaction(SIGCHLD,&act,0);
34 
35     serv_sock = socket(AF_INET,SOCK_STREAM,0);
36     memset(&serv_addr,0,sizeof(serv_addr));
37     serv_addr.sin_family = AF_INET;
38     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
39     serv_addr.sin_port = htons(atoi(argv[1]));
40 
41     if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
42         error_handling("bind error");
43     if(listen(serv_sock,5) == -1)
44         error_handling("listen error");
45     
46     while(1)
47     {
48         adr_sz = sizeof(clnt_addr);
49         clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&adr_sz);
50         if(clnt_sock == -1)
51             continue;
52         else
53             puts("connecting......");
54 
55         pid = fork();
56         if(pid == -1)
57         {
58             close(clnt_sock);
59             continue;
60         }
61         if(pid == 0)
62         {
63             close(serv_sock);
64             while((str_len = read(clnt_sock,buf,BUF_SIZE)) != 0)
65                 write(clnt_sock,buf,str_len);
66             
67             close(clnt_sock);
68             return 0;
69         }
70         else
71             close(clnt_sock);
72     }
73     close(serv_sock);
74     return 0;
75 }
76 
77 void read_childproc(int sig)
78 {
79     pid_t pid;
80     int status;
81     pid = waitpid(-1,&status,WNOHANG);
82     printf("removed proc id:%d\n",pid);
83 }
84 
85 void error_handling(char* message)
86 {
87     fputs(message,stderr);
88     fputc(\n,stderr);
89     exit(1);
90 }

 

socket学习笔记——并发服务器与I/O程序分割客户端

标签:

原文地址:http://www.cnblogs.com/boyiliushui/p/4736661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!