-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathecho_server.c
More file actions
36 lines (30 loc) · 761 Bytes
/
echo_server.c
File metadata and controls
36 lines (30 loc) · 761 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
34
35
36
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include "net/sock/udp.h"
int main(int argc, char *argv[])
{
char buf[128];
sock_udp_t sock;
sock_udp_ep_t local = { .family=AF_INET6, .port=60001 };
sock_udp_ep_t remote = { 0 };
ssize_t res = sock_udp_create(&sock, &local, NULL, 0);
if (res == -1) {
return -1;
}
while(1) {
res = sock_udp_recv(&sock, buf, sizeof(buf), -1, &remote);
if (res == -1) {
perror("recv");
return -1;
}
else {
buf[res] = '\0';
printf("res=%zu text=\"%s\"\n", res, buf);
sock_udp_send(&sock, buf, res, &remote);
}
}
return 0;
}