TCP併發伺服器之程序
阿新 • • 發佈:2019-02-01
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 }