4 #include <netinet/in.h>
6 #include <sys/socket.h>
12 handle_read(struct dsrv_context_t *ctx) {
15 struct sockaddr_storage src;
16 socklen_t srclen = sizeof(src);
17 int fd = dsrv_get_fd(ctx, DSRV_READ);
19 len = recvfrom(fd, buf, sizeof(buf), 0,
20 (struct sockaddr *)&src, &srclen);
25 printf("read %d bytes: '%*s'\n", len, len, buf);
26 if (dsrv_sendto(ctx, (struct sockaddr *)&src, srclen, 0, buf, len)
28 fprintf(stderr, "cannot add packet to sendqueue\n");
34 handle_write(struct dsrv_context_t *ctx) {
36 int fd = dsrv_get_fd(ctx, DSRV_WRITE);
39 p = ctx->rq ? nq_peek(ctx->wq) : NULL;
44 len = sendto(fd, p->buf, p->len, 0, p->raddr, p->rlen);
54 int main(int argc, char **argv) {
57 struct sockaddr_in6 listen_addr = { AF_INET6, htons(20220), 0, IN6ADDR_ANY_INIT, 0 };
59 struct sockaddr_in listen_addr = { AF_INET, htons(20220), { htonl(0x7f000001) } };
62 struct timeval timeout;
63 struct dsrv_context_t *ctx;
66 ctx = dsrv_new_context((struct sockaddr *)&listen_addr, sizeof(listen_addr),
70 fprintf(stderr, "E: cannot create server context\n");
78 dsrv_prepare(ctx, &rfds, DSRV_READ);
79 dsrv_prepare(ctx, &wfds, DSRV_WRITE);
83 timeout.tv_usec = dsrv_get_timeout(ctx);
89 result = select( FD_SETSIZE, &rfds, &wfds, 0, &timeout);
91 if (result < 0) { /* error */
94 } else if (result == 0) { /* timeout */
97 if (dsrv_check(ctx, &wfds, DSRV_WRITE))
99 else if (dsrv_check(ctx, &rfds, DSRV_READ))
105 dsrv_free_context(ctx);