TCP并发服务器源码(之进程)
1 #include <signal.h>
2 #include <stdio.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/types.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <string.h>
11 #include <stdlib.h>
12
13 #define PORT 6543
14
15 int init_sokcet() {
16 int s = socket(AF_INET,SOCK_STREAM,0);
17 if(0 > s) {
18 perror("socket");exit(-1);
19 }
20
21 /////端口重用
22 int reuseaddr =1;
23 if(0 > setsockopt
(s,SOL_SOCKET,SO_REUSEADDR,&reuseaddr,sizeof(reusea ddr))) {
24 perror("setsockopt"); exit(-1);
25 }
26
27 struct sockaddr_in addr;
28 addr.sin_family = AF_INET;
29 addr.sin_port = htons(PORT);
30 addr.sin_addr.s_addr = htonl(INADDR_ANY);
31
32 socklen_t addrlen = sizeof(addr);
33
34 if ( 0 >bind(s,(struct sockaddr *)&addr,addrlen) ) {
35 perror("bind");exit(-1);
36 }
37
38 listen(s,10);
39 return s;
40 }//init_socket
41
42
43 int rwdata(int ac_id) {
44 while(1) {
45 #define MAX 64
46 char buf[MAX];
47 bzero(buf,MAX);
48
49 int ret = read(ac_id,buf,MAX);
50 if(0 >= ret ) {
51 close(ac_id);
52 exit(-1);
53 }
54 printf("recv:%s[%d]\n",buf,ac_id);
55
56 }//while
57
58 }
59 int main(int argc,char *argv[]) {
60 int s = init_sokcet();
61
62 struct sockaddr_in addr;
63 memset(&addr,0,sizeof(addr));
64
65 socklen_t addrlen = sizeof(addr);
66
67 while(1) {
68 signal(SIGCHLD,SIG_IGN);////这里是接受子进程发送来
要推出的信
号,系统回收子进程资源前,需要子进程先通知其父进程,才能回收其资源
(因而,子
进程,继承父进程的资源(诸如:文件描苏符号,如果不通知,那么子进程
就会编程僵
尸进程))
69 int ac_id = accept(s,(struct sockaddr *)
&addr,&addrlen);
70 if (ac_id < 0) {
71 perror("accept"); exit(-1);
72 }//if
73
74 printf("new connect :socket[%d],%s:%
d\n",ac_id,inet_ntoa(add r.sin_addr),ntohs(addr.sin_port));
75
76 pid_t pid = fork();
77 if(0 > pid ) {
78 perror("fork");exit(-1);
79 } else if (0 == pid ) {
80 rwdata(ac_id);
81 exit(-1);
82 } else {
83 close(ac_id);
84 }
85
86
87
88
89 }//while
90
91
92 }
1 #include <signal.h>
2 #include <stdio.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/types.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <string.h>
11 #include <stdlib.h>
12
13 #define PORT 6543
14
15 int init_sokcet() {
16 int s = socket(AF_INET,SOCK_STREAM,0);
17 if(0 > s) {
18 perror("socket");exit(-1);
19 }
20
21 /////端口重用
22 int reuseaddr =1;
23 if(0 > setsockopt
(s,SOL_SOCKET,SO_REUSEADDR,&reuseaddr,sizeof(reusea ddr))) {
24 perror("setsockopt"); exit(-1);
25 }
26
27 struct sockaddr_in addr;
28 addr.sin_family = AF_INET;
29 addr.sin_port = htons(PORT);
30 addr.sin_addr.s_addr = htonl(INADDR_ANY);
31
32 socklen_t addrlen = sizeof(addr);
33
34 if ( 0 >bind(s,(struct sockaddr *)&addr,addrlen) ) {
35 perror("bind");exit(-1);
36 }
37
38 listen(s,10);
39 return s;
40 }//init_socket
41
42
43 int rwdata(int ac_id) {
44 while(1) {
45 #define MAX 64
46 char buf[MAX];
47 bzero(buf,MAX);
48
49 int ret = read(ac_id,buf,MAX);
50 if(0 >= ret ) {
51 close(ac_id);
52 exit(-1);
53 }
54 printf("recv:%s[%d]\n",buf,ac_id);
55
56 }//while
57
58 }
59 int main(int argc,char *argv[]) {
60 int s = init_sokcet();
61
62 struct sockaddr_in addr;
63 memset(&addr,0,sizeof(addr));
64
65 socklen_t addrlen = sizeof(addr);
66
67 while(1) {
68 signal(SIGCHLD,SIG_IGN);////这里是接受子进程发送来
要推出的信
号,系统回收子进程资源前,需要子进程先通知其父进程,才能回收其资源
(因而,子
进程,继承父进程的资源(诸如:文件描苏符号,如果不通知,那么子进程
就会编程僵
尸进程))
69 int ac_id = accept(s,(struct sockaddr *)
&addr,&addrlen);
70 if (ac_id < 0) {
71 perror("accept"); exit(-1);
72 }//if
73
74 printf("new connect :socket[%d],%s:%
d\n",ac_id,inet_ntoa(add r.sin_addr),ntohs(addr.sin_port));
75
76 pid_t pid = fork();
77 if(0 > pid ) {
78 perror("fork");exit(-1);
79 } else if (0 == pid ) {
80 rwdata(ac_id);
81 exit(-1);
82 } else {
83 close(ac_id);
84 }
85
86
87
88
89 }//while
90
91
92 }
作者:ubuntucc 发表于2013-7-31 15:19:20 原文链接
阅读:0 评论:0 查看评论