-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_socket_server.c
130 lines (104 loc) · 2.03 KB
/
unix_socket_server.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
#include <stdio.h>
#include <stdlib.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>
size_t current_time()
{
size_t t;
#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER)
static first = 1;
static LARGE_INTEGER freq;
LARGE_INTEGER tic;
if (first)
{
if (QueryPerformanceFrequency(&freq))
{
first = 0;
}
else
{
t = 0;
return t;
}
}
QueryPerformanceCounter(&tic);
t = tic.QuadPart*1E9/freq.QuadPart;
#else
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
t = tp.tv_nsec + tp.tv_sec*1E9;
#endif
return t;
}
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];
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);
while(1)
{
cfd=accept(sfd,NULL,0);
if (cfd==-1 && errno==EWOULDBLOCK )
{
sleep(1);
continue;
}
printf("[conn in]\n");
fcntl(cfd,F_SETFL,O_NONBLOCK);
while(1)
{
memset(buf,0,1024);
tic=current_time();
ret=read(cfd,buf,1024);
if (ret==-1 && errno==EWOULDBLOCK)
{
// sleep(1);
continue;
}
else if (ret==0)
{
printf("[conn out]\n");
break;
}
toc=current_time();
printf("[recv:] %lfs\n",(double)((toc-tic)*1.0e-9));
errno=0;
ret=write(cfd,buf,strlen(buf));
printf("write ret=%d errno=%d str=%s\n",ret,errno,strerror(errno));
if (ret==-1&&errno==EWOULDBLOCK)
{
continue;
}
else if (ret==0)
{
printf("[conn out\n]\n");
break;
}
}
}
}