From 5d91f9edad6e8a11e21979a6aacb4ec6f2dc63ce Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Aug 2011 19:24:36 +0300 Subject: [PATCH] Add netcat example --- .gitignore | 1 + Build | 9 +++++ examples/netcat.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 examples/netcat.cpp diff --git a/.gitignore b/.gitignore index e4bb400..bee94e5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /libmspnet.a /libmspnet.so /mspnet.pc +/netcat /pc-32-windows /release /temp diff --git a/Build b/Build index dc20f0e..899274b 100644 --- a/Build +++ b/Build @@ -20,4 +20,13 @@ package "mspnet" source "source"; install true; }; + + program "netcat" + { + source "examples/netcat.cpp"; + build_info + { + library "mspnet"; + }; + }; }; diff --git a/examples/netcat.cpp b/examples/netcat.cpp new file mode 100644 index 0000000..2532959 --- /dev/null +++ b/examples/netcat.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class NetCat: public RegisteredApplication +{ +private: + bool ipv6; + bool listen; + Net::StreamListenSocket *server_sock; + Net::StreamSocket *sock; + IO::EventDispatcher event_disp; + +public: + NetCat(int, char **); + +private: + virtual void tick(); + void net_data_available(); + void console_data_available(); +}; + +NetCat::NetCat(int argc, char **argv): + ipv6(false), + listen(false), + server_sock(0), + sock(0) +{ + GetOpt getopt; + getopt.add_option('6', "ipv6", ipv6, GetOpt::NO_ARG); + getopt.add_option('l', "listen", listen, GetOpt::NO_ARG); + getopt(argc, argv); + + const vector &args = getopt.get_args(); + if(args.empty()) + throw usage_error("host argument missing"); + + RefPtr addr = Net::resolve(args.front(), (ipv6 ? Net::INET6 : Net::INET)); + if(!listen) + { + sock = new Net::StreamSocket(addr->get_family()); + sock->connect(*addr); + event_disp.add(*sock); + sock->signal_data_available.connect(sigc::mem_fun(this, &NetCat::net_data_available)); + } + else + { + server_sock = new Net::StreamListenSocket(addr->get_family()); + server_sock->listen(*addr); + event_disp.add(*server_sock); + server_sock->signal_data_available.connect(sigc::mem_fun(this, &NetCat::net_data_available)); + } + + event_disp.add(IO::cin); + IO::cin.signal_data_available.connect(sigc::mem_fun(this, &NetCat::console_data_available)); +} + +void NetCat::tick() +{ + event_disp.tick(); + if(server_sock && sock) + { + delete server_sock; + server_sock = 0; + } +} + +void NetCat::net_data_available() +{ + if(server_sock) + { + sock = server_sock->accept(); + event_disp.add(*sock); + sock->signal_data_available.connect(sigc::mem_fun(this, &NetCat::net_data_available)); + } + else + { + char buf[1024]; + unsigned len = sock->read(buf, sizeof(buf)); + IO::cout.write(buf, len); + } +} + +void NetCat::console_data_available() +{ + char buf[1024]; + unsigned len = IO::cin.read(buf, sizeof(buf)); + if(sock) + sock->write(buf, len); +} -- 2.45.2