• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# lws minimal ws server raw adopt udp
2
3This example demonstrates echoing packets on a UDP socket in lws.
4
5A "foreign" UDP socket is created, bound (so it can "listen"), and
6adopted into lws event loop.  It acts like a tcp RAW mode connection in
7lws and uses the same callbacks.
8
9Writing is a bit different for UDP.  By default, the system has no
10idea about the receiver state and so asking for a callback_on_writable()
11always believes that the socket is writeable... the callback will
12happen next time around the event loop if there are no pending partials.
13
14With UDP, there is no "connection".  You need to write with sendto() and
15direct the packets to a specific destination.  You can learn the source
16of the last packet that arrived at the LWS_CALLBACK_RAW_RX callback by
17getting a `struct lws_udp *` from `lws_get_udp(wsi)`.  To be able to
18send back to that guy, you should take a copy of the `struct lws_udp *` and
19use the .sa and .salen members in your sendto().
20
21However the kernel may not accept to buffer / write everything you wanted to send.
22So you are responsible to watch the result of sendto() and resend the
23unsent part next time.
24
25## build
26
27```
28 $ cmake . && make
29```
30
31## usage
32
33```
34 $ ./lws-minimal-raw-adopt-udp
35$ ./lws-minimal-raw-adopt-udp
36[2018/03/24 08:12:37:8869] USER: LWS minimal raw adopt udp | nc -u 127.0.0.1 7681
37[2018/03/24 08:12:37:8870] NOTICE: Creating Vhost 'default' (no listener), 1 protocols, IPv6 off
38[2018/03/24 08:12:37:8878] USER: LWS_CALLBACK_RAW_ADOPT
39[2018/03/24 08:12:41:5656] USER: LWS_CALLBACK_RAW_RX (6)
40[2018/03/24 08:12:41:5656] NOTICE:
41[2018/03/24 08:12:41:5656] NOTICE: 0000: 68 65 6C 6C 6F 0A                                  hello.
42[2018/03/24 08:12:41:5656] NOTICE:
43```
44
45```
46 $ nc -u 127.0.0.1 7681
47hello
48hello
49```
50