Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Network::UdpSocket Class Reference

This class represent a udp connection (client and server). More...

#include <udpsocket.hh>

Inheritance diagram for Network::UdpSocket:

Network::NetSocket Network::Socket List of all members.

Public Member Functions

 UdpSocket (SOCKET_VERSION version=V4)
 UdpSocket (PROTO_KIND pkind, SOCKET_VERSION version=V4)
virtual ~UdpSocket ()
void connect (const std::string &hostname, int port)
 Here is an example of an UDP client using libsocket:.
void connect (int port)
 Here is an example of an UDP server using libsocket :.
void close ()
 Close the connection.

Protected Member Functions

std::string _read_line_bin (int socket, int &port, std::string &host, unsigned int pkg_size)
 Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol).
std::string _read_line_bin (int socket, unsigned int size)
 Get a line from socket (when used with binary protocol).

Detailed Description

This class represent a udp connection (client and server).

Author:
Julien Lemoine <speedblue at="" happycoders="" dot="" org="">

Definition at line 32 of file udpsocket.hh.


Constructor & Destructor Documentation

Network::UdpSocket::UdpSocket SOCKET_VERSION  version = V4  )  [inline]
 

Definition at line 35 of file udpsocket.hh.

References Network::UDP.

00035                                            :
00036       NetSocket(UDP, version)
00037     {}
    UdpSocket(PROTO_KIND pkind, SOCKET_VERSION version = V4) :

Network::UdpSocket::UdpSocket PROTO_KIND  pkind,
SOCKET_VERSION  version = V4
[inline]
 

Definition at line 38 of file udpsocket.hh.

References Network::UDP.

00038                                                              :
00039       NetSocket(UDP, pkind, version)
00040     {}

virtual Network::UdpSocket::~UdpSocket  )  [inline, virtual]
 

Definition at line 42 of file udpsocket.hh.

References close().

00043     {
00044       close();
00045     }


Member Function Documentation

std::string Network::UdpSocket::_read_line_bin int  socket,
unsigned int  size
[protected, virtual]
 

Get a line from socket (when used with binary protocol).

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection

Implements Network::NetSocket.

Definition at line 46 of file udpsocket.cc.

References HERE.

00047   {
00048     char                chr[MAXPKTSIZE];
00049     std::string         str = "";
00050     int                 res = 1;
00051     bool                end = false;
00052 
00053     if (socket < 0)
00054       throw NoConnection("No Socket", HERE);
00055     if (_buffer.size() >= 2 && !size)
00056       {
00057         size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
00058         _buffer = _buffer.substr(2, _buffer.size() - 2);
00059       }
00060     if (size && _buffer.size() >= size)
00061       {
00062         str = _buffer.substr(0, size);
00063         _buffer = _buffer.substr(size, _buffer.size() - size);
00064       }
00065     else
00066       while (!end)
00067         {
00068           memset(chr, 0, MAXPKTSIZE);
00069 #ifdef LIBSOCKET_WIN
00070           res = recv(socket, chr, MAXPKTSIZE, 0);
00071 #else
00072           res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
00073 #endif
00074           if (res <= 0)
00075             throw ConnectionClosed("Connection Closed", HERE);
00076           // _buffer += all octets received
00077           _buffer += std::string(chr, res);
00078           if (!size)
00079             {
00080               // extract size from _buffer and reduce it
00081               size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
00082               _buffer = _buffer.substr(2, _buffer.size() - 2);
00083             }
00084           if (_buffer.size() > size - str.size())
00085             {
00086               str += _buffer.substr(0, size - str.size());
00087               _buffer = _buffer.substr(size - str.size(), 
00088                                        _buffer.size() - size - str.size());
00089             }
00090           else
00091             {
00092               str += _buffer;
00093               _buffer = "";
00094             }
00095           if (str.size() >= size)
00096             end = true;
00097         }
00098     return str;
00099   }

std::string Network::UdpSocket::_read_line_bin int  socket,
int &  port,
std::string &  host,
unsigned int  pkg_size
[protected, virtual]
 

Get a line from socket and store client hostname and port in port and host variable (when used with binary protocol).

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection
GetpeernameError when getpeername libc function return a negative value

Implements Network::NetSocket.

Definition at line 101 of file udpsocket.cc.

References HERE, and Network::V4.

00104   {
00105     char                        chr[MAXPKTSIZE];
00106     std::string                 str = "";
00107     int                         res = 1;
00108     struct sockaddr_in          addr;
00109 #ifdef IPV6_ENABLED
00110     struct sockaddr_in6         addr6;
00111 #endif
00112 #ifdef LIBSOCKET_WIN
00113     int                         size;
00114 #else
00115     socklen_t                   size;
00116 #endif
00117     bool                        end = false;
00118 
00119 #ifdef IPV6_ENABLED
00120     if (V4 == _version)
00121 #endif
00122       size = sizeof(addr);
00123 #ifdef IPV6_ENABLED
00124     else
00125       size = sizeof(addr6);
00126 #endif
00127     if (socket < 0)
00128       throw NoConnection("No Socket", HERE);
00129     if (_buffer.size() >= 2 && !pkg_size)
00130       {
00131         pkg_size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
00132         _buffer = _buffer.substr(2, _buffer.size() - 2);
00133       }
00134     if (pkg_size && _buffer.size() >= pkg_size)
00135       {
00136         str = _buffer.substr(0, pkg_size);
00137         _buffer = _buffer.substr(pkg_size, _buffer.size() - pkg_size);
00138       }
00139     else
00140       while (!end)
00141         {
00142 #ifdef LIBSOCKET_WIN
00143           int flags = 0;
00144 #else
00145           int flags = MSG_TRUNC;
00146 #endif
00147 #ifdef IPV6_ENABLED
00148           if (V4 == _version)
00149 #endif
00150             res = recvfrom(socket, chr, MAXPKTSIZE, flags,
00151                            (struct sockaddr *) &addr, &size);
00152 #ifdef IPV6_ENABLED
00153           else
00154             res = recvfrom(socket, chr, MAXPKTSIZE, flags,
00155                            (struct sockaddr *) &addr6, &size);
00156 #endif
00157           if (res <= 0)
00158             throw ConnectionClosed("Connection Closed", HERE);
00159           // _buffer += all octets received
00160           _buffer += std::string(chr, res).substr(0, res);
00161           if (!pkg_size)
00162             {
00163               // extract size from _buffer and reduce it
00164               pkg_size = (unsigned char)_buffer[0] * 256 + 
00165                 (unsigned char)_buffer[1];
00166               _buffer = _buffer.substr(2, _buffer.size() - 2);
00167             }
00168           if (_buffer.size() > pkg_size - str.size())
00169             {
00170               str += _buffer.substr(0, pkg_size - str.size());
00171               _buffer = _buffer.substr(pkg_size - str.size(), 
00172                                        _buffer.size() - pkg_size - str.size());
00173             }
00174           else
00175             {
00176               str += _buffer;
00177               _buffer = "";
00178             }
00179           if (str.size() >= pkg_size)
00180             end = true;
00181         }
00182 #ifdef IPV6_ENABLED
00183     if (V4 == _version)
00184       {
00185 #endif
00186         host = std::string(inet_ntoa(addr.sin_addr));
00187         port = ntohs(addr.sin_port);
00188 #ifdef IPV6_ENABLED
00189       }
00190     else
00191       {
00192         char buf[INET6_ADDRSTRLEN];
00193         if (inet_ntop(AF_INET6, &addr6.sin6_addr, buf, INET6_ADDRSTRLEN) == 0)
00194           throw InetntopError("Not a valid address", HERE);
00195         host = std::string(buf);
00196         port = ntohs(addr6.sin6_port);
00197       }
00198 #endif
00199     return str;
00200   }

void Network::UdpSocket::close  ) 
 

Close the connection.

Definition at line 39 of file udpsocket.cc.

References Network::Socket::_close().

Referenced by ~UdpSocket().

00040   {
00041     if (_socket > 0)
00042       _close(_socket);
00043     _socket = 0;
00044   }

void Network::UdpSocket::connect int  port  ) 
 

Here is an example of an UDP server using libsocket :.

 #include <stdlib.h>
 #include <iostream>
 #include <string>
 #include "socket/udpsocket.hh"
 #include "exception/exception.hh"

 int main(int argc, char **argv)
 {
   Network::UdpSocket            server;
   std::string                   str, host;

   if (argc < 2)
     {
       std::cout << "Use: " << argv[0] << " port" << std::endl;
       exit(0);
     }
   try
     {
       server.connect(strtol(argv[1], NULL, 10));
       while (str != "quit")
         {
 	         //read with a timeout of 30 seconds and get client host and port
 	         str = server.read(port, host, 30);
 	         std::cout << "Received [" << str << "] from : " << host
 		           << ":" << port << "]" << std::endl;
 	       }
       server.close();
       exit (0);
     }
   catch (Network::Timeout e)
     {
       std::cerr << e;
       std::cerr << "No connection during last 30s, closing connection"
                 << std::endl;
       exit (1);
     }
   catch (Network::Exception e)
     {
       std::cerr << e;
       exit(1);
     }
 }
 

Definition at line 33 of file udpsocket.cc.

References Network::NetSocket::_bind().

00034   {
00035     _socket = _bind(port);
00036     _port = port;
00037   }

void Network::UdpSocket::connect const std::string &  hostname,
int  port
 

Here is an example of an UDP client using libsocket:.

 #include <stdlib.h>
 #include <iostream>
 #include <string>
 #include "socket/udpsocket.hh"

 int main(int argc, char **argv)
 {
   Network::UdpSocket            client;
   //Network::UdpSocket            client(Network::V6);
   // For IPV6 mode
   std::string                   str;

   if (argc < 3)
     {
       std::cout << "Use: " << argv[0] << " port hostname" << std::endl;
       exit(0);
     }
   try
     {
       client.connect(std::string(argv[2]), strtol(argv[1], NULL, 10));
       while (str != "quit")
         {
           std::cin >> str;
           client << str;
         }
       client.close();
       exit(0);
     }
   catch (Network::Exception e)
     {
       std::cerr << e;
       exit(1);
     }
 }
 

Definition at line 27 of file udpsocket.cc.

References Network::NetSocket::_bind().

00028   {
00029     _port = port;
00030     _socket = _bind(port, hostname);
00031   }


The documentation for this class was generated from the following files:
Generated on Fri Apr 8 06:13:01 2005 for libsocket by  doxygen 1.4.0