-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_socket_client.c
66 lines (50 loc) · 940 Bytes
/
unix_socket_client.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
#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>
int main(int argc,char *argv[])
{
int sfd;
char *pipename="liuwei";
struct sockaddr_un addr;
size_t len;
int cfd;
int ret;
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);
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;
ret=connect(sfd,(struct sockaddr*)(&addr),len);
while(1)
{
gets(buf,1024);
ret=write(sfd,buf,strlen(buf));
while(1)
{
ret=read(sfd,buf,strlen(buf));
if (ret==-1 )
{
continue;
}
else if (ret==0)
{
break;
}
printf(":%s\n",buf);
break;
}
}
}