WvStreams
wvudpex.cc
1/*
2 * A WvUDP example.
3 *
4 * WvUDPStream example. Waits for data on port 19.
5 * Print something like:
6 * udp<Info>: Local address is 0.0.0.0:33234 , and waits
7 */
8
9#include "wvistreamlist.h"
10#include "wvlog.h"
11#include "wvudp.h"
12
13int main(int argc, char **argv)
14{
15 WvLog err("udp", WvLog::Error);
16 WvIPPortAddr nothing;
17 WvIPPortAddr remaddr(argc > 1 ? argv[1] : "127.0.0.1:19");
18 WvUDPStream sock(nothing, nothing);
19
20 sock.enable_broadcasts();
21
22 err(WvLog::Info, "Local address is %s.\n", *sock.local());
23
24 wvcon->autoforward(sock);
25 sock.autoforward(err);
26
28 l.add_after(l.tail, wvcon, false);
29 l.add_after(l.tail, &sock, false);
30
31 while (wvcon->isok() && sock.isok())
32 {
33 sock.setdest(remaddr);
34 if (l.select(1000))
35 {
36 if (wvcon->select(0))
37 wvcon->callback();
38 else if (sock.select(0))
39 {
40 sock.callback();
41 err(WvLog::Info, " (remote: %s)\n", *sock.src());
42 }
43 }
44 }
45
46 if (!wvcon->isok() && wvcon->geterr())
47 err("stdin: %s\n", strerror(wvcon->geterr()));
48 else if (!sock.isok() && sock.geterr())
49 err("socket: %s\n", strerror(sock.geterr()));
50
51 return 0;
52}
virtual int geterr() const
If isok() is false, return the system error number corresponding to the error, -1 for a special error...
Definition wverror.h:48
An IP+Port address also includes a port number, with the resulting form www.xxx.yyy....
Definition wvaddr.h:394
WvStreamList holds a list of WvStream objects – and its select() and callback() functions know how to...
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition wvlog.h:57
virtual bool isok() const
return true if the stream is actually usable right now
Definition wvstream.cc:445
void autoforward(WvStream &s)
set the callback function for this stream to an internal routine that auto-forwards all incoming stre...
Definition wvstream.cc:362
bool select(time_t msec_timeout)
Return true if any of the requested features are true on the stream.
Definition wvstream.h:376
virtual void callback()
if the stream has a callback function defined, call it now.
Definition wvstream.cc:401
WvUDPStream can send and receive packets on a connectionless UDP socket.
Definition wvudp.h:32