サーバ側select I/O多重モデル
6873 ワード
Select
: socketmakefilestreamnullnetwork
Select
select I/O , I/O ,<span style="color:#ff0000;"> </span>。
C/C++ , , , , open,write,read , fopen -_-! 。 , 。。。。 , , -_-! , 。。。。。。
Select , IP( ), IP , , IP , IP IP , select listen , , echo 1,echo 2 。 , , , , (-_-! Ctrl-C Ctrl-V 。。。 Ubuntu vim , 。。。。)
。。。。 《Unix Network Programming》 , 。。。。 , daytime , , , 。 。。。 ANSI C , gcc -_-!
unp.h 《Unix Network Programming》 ,makefile , , Make.defines, :),makefile , 。
:
:
./TestSelectCli 127.0.0.1 1000
Conncet OK
127.0.0.1:16777343 Echo 1.
laptop:~/unpv1/unpv13e/MyTest$ ./TestSelectCli 127.0.0.1 1001
Conncet OK
127.0.0.1:16777343 Echo 2.
laptop:~/unpv1/unpv13e/MyTest$ ./TestSelectCli 192.168.0.138 1000
Conncet OK
192.168.0.138:2315299008 Echo 1.
laptop:~/unpv1/unpv13e/MyTest$ ./TestSelectCli 192.168.0.138 1001
Conncet OK
192.168.0.138:2315299008 Echo 2.
:
2315299008 Echo 1.
16777343 Echo 1.
16777343 Echo 2.
2315299008 Echo 1.
2315299008 Echo 2.
:
1 #include "unp.h"
2
3
4 void str_echo1(int connfd);
5 void str_echo2(int connfd);
6
7 int main(int argc, char **argv)
8 {
9 struct sockaddr_in cliaddr;
10 pid_t childpid;
11
12 /* Bind 1000 port to listen socket 1 */
13 int listenfd1 = Socket(AF_INET, SOCK_STREAM, 0);
14
15 struct sockaddr_in servaddr;
16 bzero(&servaddr, sizeof(servaddr));
17 servaddr.sin_family = AF_INET;
18 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
19 servaddr.sin_port = htons(1000);
20
21 Bind(listenfd1, (SA *)&servaddr, sizeof(servaddr));
22
23 Listen(listenfd1, LISTENQ);
24
25 /* Bind 1001 port to listen socket 2*/
26 int listenfd2 = Socket(AF_INET, SOCK_STREAM, 0);
27
28 struct sockaddr_in servaddr2;
29 bzero(&servaddr2, sizeof(servaddr2));
30 servaddr2.sin_family = AF_INET;
31 servaddr2.sin_addr.s_addr = htonl(INADDR_ANY);
32 servaddr2.sin_port = htons(1001);
33
34 Bind(listenfd2, (SA *)&servaddr2, sizeof(servaddr2));
35
36 Listen(listenfd2, LISTENQ);
37
38 /* Initialize fd_set struct */
39 int maxfdp1 = max(listenfd1, listenfd2) + 1;
40 fd_set rset;
41 FD_ZERO(&rset);
42
43 /* Select from this two listen socket */
44 for( ; ; )
45 {
46 FD_SET(listenfd1, &rset);
47 FD_SET(listenfd2, &rset);
48
49 int nready = -1;
50 if( (nready = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0)
51 {
52 if(EINTR == errno)
53 {
54 continue;
55 }
56 else
57 {
58 err_sys("Select error.");
59 }
60 }
61
62 /* some one listening socket is readable.*/
63 if(FD_ISSET(listenfd1, &rset))
64 {
65 socklen_t len = sizeof(cliaddr);
66 int connfd = Accept(listenfd1, (SA *)&cliaddr, &len);
67
68 if( 0 == (childpid = Fork()) )
69 {
70 /* child process */
71 Close(listenfd1);
72
73 str_echo1(connfd);
74 exit(0);
75 }
76
77 /* parent process */
78 Close(connfd);
79
80 }
81
82
83 if(FD_ISSET(listenfd2, &rset))
84 {
85 socklen_t len = sizeof(cliaddr);
86 int connfd = Accept(listenfd2, (SA *)&cliaddr, &len);
87
88 if( 0 == (childpid = Fork()) )
89 {
90 /* child process */
91 Close(listenfd2);
92
93 str_echo2(connfd);
94 exit(0);
95 }
96
97 /* parent process */
98 Close(connfd);
99
100 }
101
102 }
103
104 exit(0);
105 }
106
107 void str_echo1(int connfd)
108 {
109 struct sockaddr_in clientAddr;
110 socklen_t len = sizeof(clientAddr);
111
112 if(getpeername(connfd, (SA*) &clientAddr, &len) < 0)
113 {
114 return;
115 }
116
117 char lcBuffer[MAXLINE] = {0};
118 sprintf(lcBuffer, "%u Echo 1.", clientAddr.sin_addr.s_addr);
119
120 printf("%s/n", lcBuffer);
121
122 Write(connfd, lcBuffer, MAXLINE);
123 }
124
125
126 void str_echo2(int connfd)
127 {
128 struct sockaddr_in clientAddr;
129 socklen_t len = sizeof(clientAddr);
130
131 if(getpeername(connfd, (SA*) &clientAddr, &len) < 0)
132 {
133 return;
134 }
135
136
137 char lcBuffer[MAXLINE] = {0};
138 sprintf(lcBuffer, "%u Echo 2.", clientAddr.sin_addr.s_addr);
139
140 printf("%s/n", lcBuffer);
141
142 Write(connfd, lcBuffer, MAXLINE);
143 }
144
145
:
1 #include "unp.h"
2
3 int main(int argc, char **argv)
4 {
5 int sockfd, n;
6 char recvline[MAXLINE + 1];
7 struct sockaddr_in servaddr;
8
9 if (argc != 3)
10 err_quit("usage: a.out <IPaddress> <IPPort>");
11
12 int port = atoi(argv[2]);
13
14 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
15 err_sys("socket error");
16
17 bzero(&servaddr, sizeof(servaddr));
18 servaddr.sin_family = AF_INET;
19 servaddr.sin_port = htons(port);
20 if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
21 err_quit("inet_pton error for %s", argv[1]);
22
23 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
24 err_sys("connect error");
25
26 printf("Conncet OK/n");
27
28 while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
29 recvline[n] = 0; /* null terminate */
30
31 /* change number string to number and to ip string */
32 struct in_addr svraddr;
33 svraddr.s_addr = strtoul(recvline, NULL, 10);
34 char *pszsvraddr = inet_ntoa(svraddr);
35
36 printf("%s:%s/n", pszsvraddr, recvline);
37 }
38 if (n < 0)
39 err_sys("read error");
40
41 exit(0);
42 }