udp.cpp
Go to the documentation of this file.
00001 #include <dashel/dashel.h>
00002 #include <iostream>
00003 #include <cassert>
00004 
00005 using namespace std;
00006 using namespace Dashel;
00007 
00008 class PingServer: public Hub
00009 {
00010 public:
00011         PingServer()
00012         {
00013                 listenStream = connect("udp:port=8765");
00014         }
00015 
00016 protected:
00017         Stream* listenStream;
00018         map<Stream*, string> nicks;
00019         
00020 protected:
00021         void connectionCreated(Stream *stream) { }
00022         
00023         void incomingData(Stream *stream)
00024         {
00025                 cerr << "new data....";
00026                 PacketStream* packetStream = dynamic_cast<PacketStream*>(stream);
00027                 assert(packetStream);
00028                 IPV4Address source;
00029                 
00030                 packetStream->receive(source);
00031                 cerr << "Ping from " << source.hostname() << ":" << source.port << ": ";
00032                 
00033                 char c;
00034                 while (true)
00035                 {
00036                         packetStream->read(&c, 1);
00037                         if (c)
00038                                 cerr << c;
00039                         else
00040                                 break;
00041                 }
00042                 cerr << endl;
00043         }
00044         
00045         void connectionClosed(Stream *stream, bool abnormal) { }
00046 };
00047 
00048 class PingClient: public Hub
00049 {
00050 public:
00051         PingClient(const string& remoteTarget, const string& msg)
00052         {
00053                 PacketStream* packetStream = dynamic_cast<PacketStream*>(connect("udp:port=8766"));
00054                 assert(packetStream);
00055                 
00056                 packetStream->write(msg.c_str(), msg.length());
00057                 char c = 0;
00058                 packetStream->write(&c, 1);
00059                 
00060                 packetStream->send(IPV4Address(remoteTarget, 8765));
00061         }
00062         
00063 protected:
00064         
00065         void connectionCreated(Stream *stream) { }
00066         
00067         void incomingData(Stream *stream) { }
00068         
00069         void connectionClosed(Stream *stream, bool abnormal) { }
00070 };
00071 
00072 int main(int argc, char* argv[])
00073 {
00074         try
00075         {
00076                 if (argc > 2)
00077                 {
00078                         PingClient client(argv[1], argv[2]);
00079                 }
00080                 else if (argc > 1)
00081                 {
00082                         PingClient client(argv[1], "default message, the other side does lack creativity");
00083                 }
00084                 else
00085                 {
00086                         PingServer().run();
00087                 }
00088         }
00089         catch(DashelException e)
00090         {
00091                 std::cerr << e.what() << std::endl;
00092         }
00093         
00094         return 0;
00095 }


dashel
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:32