WvUDPStream can send and receive packets on a connectionless UDP socket.
In the constructor, the socket is attached using bind() to the given _local address. If the address is 0.0.0.0, all addresses on the local host are used; if the port is 0, an available port number is chosen automatically.
If the _rem address is 0.0.0.0, the port is not connect()ed. That means it can receive packets from anywhere and send them to anywhere. The src() and setdest() functions are useful for this. If _rem is not 0.0.0.0, connect() is called and the socket will only accept data to/from the specified remote UDP address.
Buffering: all the usual WvStream-style input buffering is available, including getline(), but because input packets may get lost it is of limited usefulness. Buffering will cause particular confusion if the socket is not connect()ed.
/* * A WvUDP example. * * WvUDPStream example. Waits for data on port 19. * Print something like: * udp<Info>: Local address is 0.0.0.0:33234 , and waits */ #include "wvistreamlist.h" #include "wvlog.h" #include "wvudp.h" int main(int argc, char **argv) { WvLog err("udp", WvLog::Error); WvIPPortAddr nothing; WvIPPortAddr remaddr(argc > 1 ? argv[1] : "127.0.0.1:19"); WvUDPStream sock(nothing, nothing); sock.enable_broadcasts(); err(WvLog::Info, "Local address is %s.\n", *sock.local()); wvcon->autoforward(sock); sock.autoforward(err); WvIStreamList l; l.add_after(l.tail, wvcon, false); l.add_after(l.tail, &sock, false); while (wvcon->isok() && sock.isok()) { sock.setdest(remaddr); if (l.select(1000)) { if (wvcon->select(0)) wvcon->callback(); else if (sock.select(0)) { sock.callback(); err(WvLog::Info, " (remote: %s)\n", *sock.src()); } } } if (!wvcon->isok() && wvcon->geterr()) err("stdin: %s\n", strerror(wvcon->geterr())); else if (!sock.isok() && sock.geterr()) err("socket: %s\n", strerror(sock.geterr())); return 0; }