-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagramUnixClient.c
61 lines (53 loc) · 1.2 KB
/
datagramUnixClient.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
/*
* datagramUnixClient.c
*/
// This program connects to a datagram 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_DGRAM, 0);
if (sockFd < 0)
{
perror("socket");
exit(1);
}
// set up the destination socket
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, socketPath);
struct sockaddr *ptr = (struct sockaddr *) &addr;
int len = sizeof(addr);
int bytesRead = 10;
char buf[100];
int retVal;
while (bytesRead > 0)
{
// read in from stdin and send
bytesRead = read(0, buf, 100);
if (bytesRead < 0)
{
perror("read");
exit(1);
}
retVal = sendto(sockFd, buf, bytesRead, 0, ptr, len);
if (retVal < 0)
{
perror("sendto");
exit(1);
}
}
int ret = close(sockFd);
if (ret < 0)
{
perror("close");
exit(1);
}
}