From: tho Date: Fri, 13 Apr 2007 14:34:52 +0000 (+0000) Subject: added new example: a simple TCP Client-Server X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=52dcdfccdc3c371bc9d884935ad348755fd38e55;p=senf.git added new example: a simple TCP Client-Server git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@226 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Examples/TCPClientServer/SConscript b/Examples/TCPClientServer/SConscript new file mode 100644 index 0000000..5cb8fd5 --- /dev/null +++ b/Examples/TCPClientServer/SConscript @@ -0,0 +1,10 @@ +Import('env') +import SENFSCons + +########################################################################### + +SENFSCons.Binary(env, 'client', 'client.cc', + LIBS = [ 'Packets', 'Socket', 'Utils' ]); + +SENFSCons.Binary(env, 'server', 'server.cc', + LIBS = [ 'Scheduler', 'Packets', 'Socket', 'Utils' ]); diff --git a/Examples/TCPClientServer/client.cc b/Examples/TCPClientServer/client.cc new file mode 100644 index 0000000..0297c41 --- /dev/null +++ b/Examples/TCPClientServer/client.cc @@ -0,0 +1,53 @@ +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +// Custom includes +#include +#include +#include "Socket/TCPSocketHandle.hh" +#include "Socket/INetAddressing.hh" +#include "Packets/EthernetPacket.hh" + + +int main(int argc, char const * argv[]) +{ + try { + for (int i=0; i<=1000; i++) { + senf::TCPv4ClientSocketHandle sock; + sock.connect(senf::INet4Address("127.0.0.1", 4243)); + sock.protocol().linger(true); + + std::stringstream s; + s << i++; + sock.write(s.str()); + + sock.close(); + + std::cout << i << std::endl; + } + } + catch (std::exception const & ex) { + std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n"; + } + + return 0; +} diff --git a/Examples/TCPClientServer/server.cc b/Examples/TCPClientServer/server.cc new file mode 100644 index 0000000..99991f7 --- /dev/null +++ b/Examples/TCPClientServer/server.cc @@ -0,0 +1,85 @@ +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// Definition of non-inline non-template functions + +//#include "server.hh" +//#include "server.ih" + +// Custom includes +#include +#include "Scheduler/Scheduler.hh" +#include "Utils/membind.hh" +#include "Socket/TCPSocketHandle.hh" +#include "Socket/INetAddressing.hh" +#include "Packets/EthernetPacket.hh" + + +class Server +{ + senf::TCPv4ServerSocketHandle serverSock; + +public: + Server(std::string const & host, unsigned int port) + : serverSock(senf::INet4Address(host, port)) {} + + void run() + { + senf::Scheduler::instance().add( + serverSock, + senf::membind(&Server::accept, this), + senf::Scheduler::EV_READ); + senf::Scheduler::instance().process(); + } + +private: + void accept(senf::FileHandle /* ignored */, senf::Scheduler::EventId event) + { + senf::TCPv4ClientSocketHandle clientSock (serverSock.accept()); + senf::Scheduler::instance().add( + clientSock, + senf::membind(&Server::readFromClient, this), + senf::Scheduler::EV_READ); + } + + void readFromClient(senf::TCPv4ClientSocketHandle clientSock, senf::Scheduler::EventId event) + { + if (!clientSock) { + senf::Scheduler::instance().remove(clientSock); + return; + } + std::string data (clientSock.read()); + std::cout << "'" << data << "'" << std::endl; + } +}; + + +int main(int argc, char const * argv[]) +{ + try { + Server myServer ("127.0.0.1", 4243); + myServer.run(); + } + catch (std::exception const & ex) { + std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n"; + } + return 0; +}