summaryrefslogtreecommitdiff
path: root/src/tests/udpsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/udpsocket.cpp')
-rw-r--r--src/tests/udpsocket.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/tests/udpsocket.cpp b/src/tests/udpsocket.cpp
new file mode 100644
index 0000000..2a74acf
--- /dev/null
+++ b/src/tests/udpsocket.cpp
@@ -0,0 +1,86 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/udpsocket.h"
9#include "bu/sio.h"
10
11#include <errno.h>
12#include <arpa/inet.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <sys/utsname.h>
16
17using namespace Bu;
18
19int main( int argc, char *argv[] )
20{
21 sio << Fmt::hex(8) << INADDR_ANY << sio.nl << Fmt::hex(8) << inet_addr("0.0.0.0") << sio.nl;
22 if( argc == 1 )
23 {
24 sio << "Options are 'l' for listening and 'b' for broadcasting."
25 << sio.nl;
26 }
27 else if( argv[1][0] == 'l' )
28 {
29 sio << "Listening..." << sio.nl;
30 Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast );
31
32 for(;;)
33 {
34 char buf[1501];
35 int iRead = udp.read( buf, 1500 );
36 if( iRead >= 0 )
37 {
38 buf[iRead] = '\0';
39 sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl;
40 }
41 else
42 {
43 sio << "Got " << iRead << ": " << strerror( errno ) << sio.nl;
44 }
45 }
46 }
47 else if( argv[1][0] == 'L' )
48 {
49 sio << "Listening..." << sio.nl;
50 Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast );
51
52 for(;;)
53 {
54 char buf[1501];
55 Bu::UdpSocket::addr aHost;
56 int iPort;
57 int iRead = udp.read( buf, 1500, aHost, iPort );
58 if( iRead >= 0 )
59 {
60 buf[iRead] = '\0';
61 sio << "Read(" << iRead << ") from " << Bu::UdpSocket::addrToStr( aHost ) << ":" << iPort << ": '" << buf << "'" << sio.nl;
62 }
63 }
64 }
65 else if( argv[1][0] == 'b' )
66 {
67 sio << "Broadcasting..." << sio.nl;
68 Bu::UdpSocket udp("255.255.255.255", 6688,
69 UdpSocket::Write|UdpSocket::Broadcast );
70
71 for(;;)
72 {
73 int iWrote = udp.write("hello", 5 );
74 sio << "Wrote(" << iWrote << "): " << strerror( errno ) << sio.nl;
75 usleep( 250000 );
76 }
77 }
78 else
79 {
80 sio << "Options are 'l' for listening and 'b' for broadcasting."
81 << sio.nl;
82 }
83
84 return 0;
85}
86