Skip to content
Snippets Groups Projects
Commit 1e6ad112 authored by tho's avatar tho
Browse files

added new example: a simple TCP Client-Server

parent 86187a80
No related branches found
No related tags found
No related merge requests found
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' ]);
// $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 <iostream>
#include <sstream>
#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;
}
// $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 <string>
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment