-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_select.c
122 lines (107 loc) · 2.23 KB
/
socket_select.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
#include <helper.h>
int main(int argc,char *argv[])
{
int sfd;
struct sockaddr_in addr,caddr;
size_t len;
socklen_t clen;
int cfd;
int ret;
size_t tic,toc;
char buf[1024];
int i,nfds,nact;
fd_set rfds;
int *watch,nwatch;
sfd=esocket(AF_INET,SOCK_STREAM,0);
fcntl(sfd,F_SETFL,O_NONBLOCK);
addr=mkaddr("10.132.128.220",8090);
len=sizeof(addr);
ebind(sfd,&addr,len);
elisten(sfd,1);
/* watched fd */
watch=(int*)malloc(sizeof(int)*1024);
memset(watch,0,sizeof(int)*1024);
/* Zero out all fds */
watch[sfd]=1;
while (1)
{
/* Set fd_set */
nfds=0;
FD_ZERO(&rfds);
for (i=0;i<1024;++i)
{
if ( watch[i] )
{
nfds=i;
FD_SET(i,&rfds);
}
}
nact=select(nfds+1,&rfds,NULL,NULL,NULL);
if (nact>=0)
{
for (i=0;i<=nfds;++i)
{
/* New Connection or Read Avaliable */
if ( FD_ISSET(i,&rfds) )
{
/* New Connection */
if (i==sfd)
{
printf("[new connection]\n");
clen=sizeof(caddr);
/* Accept new connection */
cfd=accept(sfd,(struct sockaddr*)(&caddr),&clen);
fcntl(cfd,F_SETFL,O_NONBLOCK);
/* Add to watch list */
watch[cfd]=1;
}
else /* Read Avaliable */
{
printf("[write ]\n");
while (1)
{
memset(buf,0,1024);
ret=read(i,buf,1000);
/* read complete */
if (ret==-1 && ( errno==EWOULDBLOCK ||errno==EAGAIN ))
{
break;
}
else if (ret==0) /* remote closed */
{
watch[i]=0;
printf("[closed]\n");
break;
}
printf("==%s\n",buf);
sprintf(buf,"HTTP/1.1 200 OK\r\n");
sprintf(buf,"HTTP/1.1 204 OK\r\nDate: Sat, 31 Dec 2005 23:59:59 GMT\r\nContent-Type: text/html;\r\nContent-Length: 12\r\n\r\n<html>yes </html>");
write(i,buf,strlen(buf));
break;
}
}
}
}
}
else
{
fprintf(stderr,"Error: select error\n");
fprintf(stderr," %s\n",strerror(errno));
exit(0);
}
}
return 0;
}