forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_echo_server.c
44 lines (40 loc) · 1.11 KB
/
udp_echo_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
/*
* udp echo server
*
* @build make examples
* @server bin/udp_echo_server 1234
* @client bin/nc -u 127.0.0.1 1234
* nc -u 127.0.0.1 1234
*
*/
#include "hloop.h"
#include "hsocket.h"
static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
printf("on_recvfrom fd=%d readbytes=%d\n", hio_fd(io), readbytes);
char localaddrstr[SOCKADDR_STRLEN] = {0};
char peeraddrstr[SOCKADDR_STRLEN] = {0};
printf("[%s] <=> [%s]\n",
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
printf("< %.*s", readbytes, (char*)buf);
// echo
printf("> %.*s", readbytes, (char*)buf);
hio_write(io, buf, readbytes);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
return -10;
}
int port = atoi(argv[1]);
hloop_t* loop = hloop_new(0);
hio_t* io = hloop_create_udp_server(loop, "0.0.0.0", port);
if (io == NULL) {
return -20;
}
hio_setcb_read(io, on_recvfrom);
hio_read(io);
hloop_run(loop);
hloop_free(&loop);
return 0;
}