-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestPOLL.c
168 lines (149 loc) · 4.82 KB
/
testPOLL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int startup( int port )
{
// 1. Create sockets
int sock = socket(AF_INET,SOCK_STREAM,0);//The second parameter here represents TCP
if( sock < 0 )
{
perror("socket fail...\n");
exit(2);
}
// 2. When TIME_WAIT is resolved, the server cannot be restarted; the server can be restarted immediately.
int opt = 1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);// Address of any type
local.sin_port = htons(port);// The port number here can also be specified directly as 8080.
// 3. Binding port number
if( bind(sock,(struct sockaddr *)&local,sizeof(local)) < 0 )
{
perror("bind fail...\n");
exit(3);
}
// 4. Get a listening socket
if( listen(sock,5) < 0 )
{
perror("listen fail...\n");
exit(4);
}
return sock;
}
int main(int argc,char* argv[] )
{
if( argc != 2 )
{
printf("Usage:%s port\n ",argv[0]);
return 1;
}
// 1. Get a listening socket
int listen_sock = startup(atoi(argv[1]));//Port numbers are passed in as strings and need to be converted to integers
// 2. Initialization of Structures - List of Structures to Monitor
struct pollfd fd_list[1024];
int num = sizeof(fd_list)/sizeof(fd_list[0]);
int i = 0;
for(; i < num ; i++ )
{
fd_list[i].fd = -1;// File descriptor
fd_list[i].events = 0;// Set of events to monitor
fd_list[i].revents = 0;// Ready Event Set of Concerned Descriptors
}
// 3. Add read-only events for file descriptors of interest
i = 0;
for( ; i < num; i++ ){
if( fd_list[i].fd == -1 ){
fd_list[i].fd = listen_sock;
fd_list[i].events = POLLIN;// Concern about Read-Only Events
break;
}
}
while(1)
{
//4. Start calling poll and wait for the file descriptor set of interest to be ready
switch( poll(fd_list,num,3000) )
{
case 0:// The state of the denominator has exceeded before it has changed. timeout Time
printf("timeout...\n");
continue;
case -1:// failed
printf("poll fail...\n");
continue;
default: // Succeed
{
// If it is a listener file descriptor, call accept to accept a new connection
// If it is a normal file descriptor, read is called to read the data
int i = 0;
for( ;i < num; i++ )
{
if( fd_list[i].fd == -1 )
{
continue;
}
if( fd_list[i].fd == listen_sock && ( fd_list[i].revents & POLLIN ))
{
// 1. Provide a connection acceptance service if the listening socket is ready to read
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock,(struct sockaddr *)&client,&len);
if(new_sock < 0)
{
perror("accept fail...\n ");
continue;
}
//After obtaining the new file descriptor, add the file descriptor to the array for the next time you care about the file descriptor
int i = 0;
for( ; i < num; i++ )
{
if( fd_list[i].fd == -1 )//Place the first value in the array at - 1
break;
}
if( i < num )
{
fd_list[i].fd= new_sock;
fd_list[i].events = POLLIN;
}
else
{
close(new_sock);
}
printf("get a new link![%s:%d]\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
continue;
}
//2. At this point, we are concerned with ordinary file descriptors.
// Provide services to read data at this time
if( fd_list[i].revents & POLLIN )
{
char buf[1024];
ssize_t s = read(fd_list[i].fd,buf,sizeof(buf)-1);
if( s < 0 )
{
printf("read fail...\n");
continue;
}
else if( s == 0 )
{
printf("client quit...\n");
close(fd_list[i].fd);
fd_list[i].fd = -1;
}
else
{
buf[s] = 0;
printf("client# %s\n",buf);
}
}
}
}
break;
}
}
return 0;
}