forked from coolaj86/libev-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp-echo-server.c
61 lines (49 loc) · 1.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Source: http://www.mail-archive.com/[email protected]/msg00987.html
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ev.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <unistd.h>
#define DEFAULT_PORT 3333
#define BUF_SIZE 4096
// Lots of globals, what's the best way to get rid of these?
int sd; // socket descriptor
struct sockaddr_in addr;
int addr_len = sizeof(addr);
char buffer[BUF_SIZE];
// This callback is called when data is readable on the UDP socket.
static void udp_cb(EV_P_ ev_io *w, int revents) {
puts("udp socket has become readable");
socklen_t bytes = recvfrom(sd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*) &addr, (socklen_t *) &addr_len);
// add a null to terminate the input, as we're going to use it as a string
buffer[bytes] = '\0';
printf("udp client said: %s", buffer);
// Echo it back.
// WARNING: this is probably not the right way to do it with libev.
// Question: should we be setting a callback on sd becomming writable here instead?
sendto(sd, buffer, bytes, 0, (struct sockaddr*) &addr, sizeof(addr));
}
int main(void) {
int port = DEFAULT_PORT;
puts("udp_echo server started...");
// Setup a udp listening socket.
sd = socket(PF_INET, SOCK_DGRAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0)
perror("bind");
// Do the libev stuff.
struct ev_loop *loop = ev_default_loop(0);
ev_io udp_watcher;
ev_io_init(&udp_watcher, udp_cb, sd, EV_READ);
ev_io_start(loop, &udp_watcher);
ev_loop(loop, 0);
// This point is never reached.
close(sd);
return EXIT_SUCCESS;
}