4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 /** \mainpage UDP Client/Server example application
25 \dontinclude udpServer.cc
27 This Application is a command line based client/server application, which sends some strings from
28 client to server, where they are printed on the command line.
30 After installing the Library, the udpServer and the udpClient can be found in the
31 senf/Example/udpServer directory and compiled with
35 <Then you can start the client/server with>
41 When we take a look to the code, we start with the Server:
42 First we include the necessary headers:
44 \skip // Custom includes
47 The Scheduler will be needed because we implement a non blocking UDP Server with the %senf
48 integrated Scheduler. The scheduler library provides a simple yet flexible abstraction of
49 the standard asynchronous UNIX mainloop utilizing \c select or \c poll.
51 \section UDP_serverApplication UDP server application
53 First we define a class which is responsible for opening a socket and print out the incoming
54 data on stdout. We create a \c ::UDPv4ClientSocketHandle, which is an unconnected and
55 uninitialized UDP (Ipv4) socket.
59 The constructor initialize the Server Object with a given address and port. In our case the
60 server listens static on the loopback device with port 4243.
64 The public \c run() member is called to run the sniffer. It first adds the socket to the
65 Scheduler. The <tt> \link senf::Scheduler::add add() \endlink </tt> call takes two Arguments,
66 the socket to bind to (which can be a lot of things and must not necessarily be a socket
67 instance) and callback function to call, whenever there is an event on that socket.The callback
68 is specified as a <a href="http://www.boost.org/doc/html/function.html">Boost.Function</a> object.
69 A third argument may be specified to restrict the events, on which the function is called, here we
70 used the EV_READ Argument, because we just want the program to read from the socket.
71 The default argument is set to \c senf::Scheduler::EV_ALL, which allows all actions on the socket.
75 Calling the Schedulers <tt> \link senf::Scheduler::process process()\endlink </tt> method will
76 start the event loop. This call does not return (ok, it does return in special cases if
77 \c senf::Scheduler::terminate() is called which does not apply here). The Callback Function is
78 the \c readFromClient() Function, which is declared as private here and will be called whenever
79 an event on the socket is encountered. The scheduler passes the event ID to the function.
83 In the function the data from the socket is put into a standard string and dumped out on stdout.
87 In the main function we need to create an Object of our Server with the loopback address and the port.
91 That's it. We finish of by catching the exception and giving as much %detail as possible if an
92 exception is caught. The \c prettyName function from the \c Utils library is used, to get a nice,
93 printable representation of the dynamic type of the exception instance. It is an interface to
94 the g++ demangler. This is necessary since the name member of the C++ \c type_info instance is
95 a mangled name in g++.
97 \section UDP_clientApplication UDP client application
99 \dontinclude udpClient.cc
101 The client application uses the same mechanisms, but implements them in a small main function.
102 It sends numbers as strings to the server.
107 First a \c ::UDPv4ClientSocketHandle is created. With the function
108 \c writeto(senf::INet4SocketAddress, string) the string s will be written to the specified
109 address and port, which is constructed here from a static string read from the console with the format \c IP:PORT. In this
110 example Integers from zero to ten are send to the Server socket.
112 The exception handling is again the same as with the server application.
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"