From c259f95bd0e58b247940a339bb9b4b401b4e9438 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 20 Jan 2011 17:48:45 +0000 Subject: UdpSocket is pretty much working. Non-blocking mode isn't, which is odd, but we can figure that out later. --- src/tests/udpsocket.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src/tests') diff --git a/src/tests/udpsocket.cpp b/src/tests/udpsocket.cpp index 3038560..18c7bb9 100644 --- a/src/tests/udpsocket.cpp +++ b/src/tests/udpsocket.cpp @@ -2,22 +2,57 @@ #include "bu/sio.h" #include +#include +#include +#include +#include using namespace Bu; int main( int argc, char *argv[] ) { - if( argv[1][0] == 'l' ) + sio << Fmt::hex(8) << INADDR_ANY << sio.nl << Fmt::hex(8) << inet_addr("0.0.0.0") << sio.nl; + if( argc == 1 ) + { + sio << "Options are 'l' for listening and 'b' for broadcasting." + << sio.nl; + } + else if( argv[1][0] == 'l' ) { sio << "Listening..." << sio.nl; - Bu::UdpSocket udp( "255.255.255.255", 6688, UdpSocket::Read|UdpSocket::Broadcast ); + Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast ); for(;;) { char buf[1501]; int iRead = udp.read( buf, 1500 ); - buf[iRead] = '\0'; - sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl; + if( iRead >= 0 ) + { + buf[iRead] = '\0'; + sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl; + } + else + { + sio << "Got " << iRead << ": " << strerror( errno ) << sio.nl; + } + } + } + else if( argv[1][0] == 'L' ) + { + sio << "Listening..." << sio.nl; + Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast ); + + for(;;) + { + char buf[1501]; + Bu::UdpSocket::addr aHost; + int iPort; + int iRead = udp.read( buf, 1500, aHost, iPort ); + if( iRead >= 0 ) + { + buf[iRead] = '\0'; + sio << "Read(" << iRead << ") from " << Bu::UdpSocket::addrToStr( aHost ) << ":" << iPort << ": '" << buf << "'" << sio.nl; + } } } else if( argv[1][0] == 'b' ) -- cgit v1.2.3