54a2040c6f75f1ec4678fcb1d9fd67aae30a0c8c
[senf.git] / Examples / TCPClientServer / server.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 // Definition of non-inline non-template functions
23
24 //#include "server.hh"
25 //#include "server.ih"
26
27 // Custom includes
28 #include <string>
29 #include <iostream>
30 #include "Scheduler/Scheduler.hh"
31 #include "Utils/membind.hh"
32 #include "Socket/Protocols/INet/TCPSocketHandle.hh"
33 #include "Socket/Protocols/INet/INetAddressing.hh"
34
35
36 class Server
37 {
38     senf::TCPv4ServerSocketHandle serverSock;
39
40 public:
41     Server(senf::INet4Address const & host, unsigned int port)
42         : serverSock(senf::INet4SocketAddress(host, port)) {}
43     
44     void run() 
45     {
46         senf::Scheduler::instance().add(
47             serverSock, 
48             senf::membind(&Server::accept, this),
49             senf::Scheduler::EV_READ);
50         senf::Scheduler::instance().process();
51     }
52          
53 private:
54     void accept(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
55     {
56         senf::TCPv4ClientSocketHandle clientSock (serverSock.accept());
57         senf::Scheduler::instance().add(
58             clientSock,
59             senf::membind(&Server::readFromClient, this),
60             senf::Scheduler::EV_READ);
61     }
62     
63     void readFromClient(senf::TCPv4ClientSocketHandle clientSock, senf::Scheduler::EventId event)
64     {
65         if (!clientSock) {
66             senf::Scheduler::instance().remove(clientSock);
67             return;
68         }
69         std::string data (clientSock.read());
70         std::cout << "'" << data << "'" << std::endl;
71     }
72 };
73
74
75 int main(int argc, char const * argv[])
76 {
77     try {
78         Server myServer (senf::INet4Address::Loopback, 4243);
79         myServer.run();
80     }
81     catch (std::exception const & ex) {
82         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
83     }
84     return 0;
85 }
86
87 \f
88 // Local Variables:
89 // mode: c++
90 // fill-column: 100
91 // c-file-style: "senf"
92 // indent-tabs-mode: nil
93 // ispell-local-dictionary: "american"
94 // compile-command: "scons -u"
95 // comment-column: 40
96 // End: