-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_socket_select.c
133 lines (110 loc) · 2.15 KB
/
unix_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
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
int main(int argc,char *argv[])
{
int sfd;
char *pipename="liuwei";
struct sockaddr_un addr;
size_t len;
int cfd;
int ret;
size_t tic,toc;
char buf[1024];
int i,nfds,nact;
fd_set rfds;
int *watch,nwatch;
sfd=socket(AF_UNIX,SOCK_STREAM,0);
if (sfd<0)
{
fprintf(stderr,"socket error");
return 0;
}
fcntl(sfd,F_SETFL,O_NONBLOCK);
unlink(pipename);
memset(&addr,0,sizeof(addr));
addr.sun_family=AF_UNIX;
sprintf(addr.sun_path,"%s",pipename);
len=offsetof(struct sockaddr_un,sun_path)+strlen(addr.sun_path)+1;
bind(sfd,(struct sockaddr*)(&addr),len);
listen(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");
/* Accept new connection */
cfd=accept(sfd,NULL,0);
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);
write(i,buf,strlen(buf));
break;
}
}
}
}
}
else
{
fprintf(stderr,"Error: select error\n");
fprintf(stderr," %s\n",strerror(errno));
exit(0);
}
}
return 0;
}