-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamUnixClient.c
63 lines (55 loc) · 1.2 KB
/
streamUnixClient.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
/*
* streamUnixClient.c
*/
// This program connects to a stream UNIX domain socket
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *socketPath = "named";
// create the socket
int sockFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockFd < 0)
{
perror("socket");
exit(1);
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
int size = sizeof(addr.sun_path);
strncpy(addr.sun_path, socketPath, size - 1);
// connect the socket to the listening socket
size = sizeof(addr);
int ret = connect(sockFd, (struct sockaddr*) &addr, size);
if (ret < 0)
{
perror("connect");
exit(1);
}
int bytesRead = 10;
char buf[100];
// read in from stdin and send
bytesRead = read(0, buf, 100);
if (bytesRead < 0)
{
perror("read");
exit(1);
}
ret = send(sockFd, buf, bytesRead, 0);
if (ret < 0)
{
perror("send");
exit(1);
}
ret = close(sockFd);
if (ret < 0)
{
perror("close");
exit(1);
}
}