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
28 from 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
37 Now you can start the application with
43 \section UDPserverApplication UDP server application
45 We take a look to the code starting with the Server: The file starts out including the necessary
48 \skip // Custom includes
51 The Scheduler will be needed because we implement a non blocking UDP Server with the %senf
52 integrated Scheduler. The scheduler library provides a simple yet flexible abstraction of
53 the standard asynchronous UNIX mainloop utilizing \c select or \c poll.
55 First we define a class which is responsible for opening a socket and print out the incoming
56 data on stdout. We create a \c ::UDPv4ClientSocketHandle, which is an unconnected and
57 uninitialized UDP (IPv4) socket.
61 The name \e client socket handle is a bit misleading: The handle is a \e client and not a \e
62 server socket handle since it implements the ordinary (client) socket API and not the connection
63 oriented (e.g. TCP accept) server socket API. Since UDP is not connection oriented, there exists
64 no \c UDPv4ServerSocketHandle.
68 The constructor initialize the Server Object with a given address and port. In our case the
69 server configuration is static: The server listens on the loopback device on port 4243. We
70 instantiate and configure a senf::scheduler::FdEvent instance to call Server::readFromClient
71 whenever data is available on the handle.
75 The public \c run() member is called to run the sniffer. It enables the event callback and
76 enters the scheduler main-loop.
80 This member function is called by the %scheduler whenever new data is available. The scheduler
81 passes in an event-mask of the event(s) triggering the call.
85 In the function the data from the socket is put into a standard string and dumped out on stdout.
90 In the main function we need to create an Object of our Server with the loopback address and the
93 That's it. We finish of by catching the exception and giving as much %detail as possible if an
94 exception is caught. The \c prettyName function from the \c Utils library is used, to get a
95 nice, printable representation of the dynamic type of the exception instance. It is an interface
96 to the g++ de-mangler. This is necessary since the name member of the C++ \c type_info instance
97 is a mangled name in g++.
99 \section UDPclientApplication UDP client application
101 \dontinclude udpClient.cc
103 The client application uses the same mechanisms but implements them in a small main function.
104 It sends numbers as strings to the server.
110 First a \c senf::::UDPv4ClientSocketHandle is created. With
111 <tt>writeto(senf::INet4SocketAddress, string)</tt> the string \c s will be written to the
112 specified address and port, which is constructed here from a static string read from the command
113 line with the syntax \c IP:PORT. In this example Integers from zero to ten are send to the
121 // comment-column: 40
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"