forked from denehs/unix-domain-socket-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
46 lines (38 loc) · 913 Bytes
/
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
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "ipc.h"
int main() {
struct sockaddr_un addr;
struct sockaddr_un from;
socklen_t fromlen = sizeof(from);
char buf[8192];
int ret;
int len;
int fd;
if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
perror("socket()");
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SERVER_SOCK_FILE);
unlink(SERVER_SOCK_FILE);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
close(fd);
perror("bind()");
return 1;
}
while ((len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fromlen)) > 0) {
printf("received %d bytes: %s\n", len, buf);
strcpy(buf, "transmit good!");
ret = sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr *)&from, fromlen);
if (ret < 0) {
perror("sendto()");
break;
}
}
close(fd);
return 0;
}