a7abd7947d77bf4b65c9ed519cdfb6f6200fcf6c
[senf.git] / Examples / UDPClientServer / Mainpage.dox
1 // $Id: Mainpage.dox 625 2008-01-16 12:00:00Z Pug $
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \mainpage UDP Client/Server example application 
24         
25         \dontinclude udpServer.cc
26         
27         This Application is a command line based client/server application, which sends some strings from client to server, where they are printed on the command line.
28
29         After installing the Library,  the udpServer and the udpClient can be found in the senf/Example/udpServer directory and compiled with 
30         
31         <pre>
32                 #scons -u
33                 <Then you can start the client/server with>
34
35                 #./udpServer
36                 #./udpClient
37         </pre>
38         
39         When we take a look to the code, we start with the Server: 
40         First we include the necessary headers: 
41         
42         \skip // Custom includes
43         \until membind
44         
45         The Scheduler will be needed because we implement a non blocking UDP Server with the senf integrated Scheduler. The  scheduler library provides a simple yet flexible abstraction of the standard asynchronous UNIX mainloop utilizing \c select or \c poll.
46         
47         \section UDP_serverApplication UDP server application
48         
49         First we define a class which is responsible for opening a socket and print out the incoming data on stdout. 
50         We create a \c UDPv4ClientSocketHandle, which is an unconnected and uninitialized UDP (Ipv4) socket.
51         
52         \until serverSock;
53         
54         The constructor initialize the Server Object with a given address and port. In our case the server listens static on the loopback device with port 4243.
55         
56         \until {}
57         
58         The public \c run() member is called to run the sniffer. It first adds the socket to the Scheduler. The \c add() call takes two Arguments, the socket to bind to (which can be a lot of things and must not necessarily be a socket instance) and callback function to call, whenever there is an event on that socket.The callback is specified as a <a href="http://www.boost.org/doc/html/function.html">Boost.Function</a> object. A third argument may be specified to restrict the events, on which the function is called, here we used the EV_READ Argument, because we just want the program to read from the socket. The default argument is set to \c senf::Scheduler::EV_ALL, which allows all actions on the socket.
59         
60         \until }
61         
62         Calling the Schedulers \c process() method will start the event loop. This call does not return (ok, it does return in special cases if \c senf::Scheduler::terminate() is called which does not apply here).
63         The Callback Function is the \c readFromClient() Function, which is declared as private here and will be called whenever an event on the socket is encountered. The scheduler passes the event ID to the function.
64         
65         \until event)
66         
67         In the function the data from the socket is put into a standard string and dumped out on stdout. 
68         
69         \until };
70         
71         In the main function we need to create an Object of our Server with the loopback address and the port.
72         
73         \until return 0;
74         
75         That's it. We finish of by catching the exception and giving as much detail as possible if an exception is caught. The \c prettyName function from the \c Utils library is used, to get a nice, printable representation of the dynamic type of the exception instance. It is an interface to the g++ demangler. This is necessary since the name member of the C++ \c type_info instance is a mangled name in g++.
76         
77         \section UDP_clientApplication UDP client application
78         
79         \dontinclude udpClient.cc
80         
81         The client application uses the same mechanisms, but implements them in a small main function. It sends numbers as strings to the server. 
82         
83         \skip  argv[])
84         \until return 0;
85         
86         First a \c UDPV4CLIENTSOCKETHANDLE is created. With the function \c writeto(senf::INet4SocketAddress, string) the string s will be written to the specified address and port, which is constructed here from a  static string \c "127.0.0.1:4243". In this example Integers from zero to ten are send to the Server socket. 
87
88         The exception handling is again the same as with the server application.
89 */      
90         
91
92 // Local Variables:
93 // mode: c++
94 // fill-column: 100
95 // comment-column: 40
96 // c-file-style: "senf"
97 // indent-tabs-mode: nil
98 // ispell-local-dictionary: "american"
99 // compile-command: "scons -u test"
100 // mode: flyspell
101 // mode: auto-fill
102 // End: