-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
33 lines (32 loc) · 892 Bytes
/
server.c
File metadata and controls
33 lines (32 loc) · 892 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
int main(){
char buf[256];
int bytes;
socklen_t socksize = sizeof(struct sockaddr_un);
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
struct sockaddr_un saddr, faddr;
memset(&saddr, 0, sizeof(struct sockaddr_un));
saddr.sun_family = AF_UNIX;
saddr.sun_path[0] = '\0';
strcpy(&saddr.sun_path[1], "test.sock");
bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
while(1){
bytes = recvfrom(sfd, buf, 256, 0, (struct sockaddr*)&faddr, &socksize);
if(bytes > 1){
printf("[%s]", &faddr.sun_path[1]);
printf("%s\n", buf);
if(strncmp("end", buf, 4) == 0){
break;
}
}
}
close(sfd);
return 0;
}