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)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection
GetpeernameError when getpeername libc function return a negative value.

std::string _read_line_bin (int socket, unsigned int size)
 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.


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.

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

Definition at line 38 of file udpsocket.hh.

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

Definition at line 42 of file udpsocket.hh.

References close().


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 Network::Socket::_buffer, and HERE.

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 Network::Socket::_buffer, Network::Socket::_version, HERE, and Network::V4.

void Network::UdpSocket::close (  ) 

Close the connection.

Definition at line 39 of file udpsocket.cc.

References Network::Socket::_close(), and Network::Socket::_socket.

Referenced by ~UdpSocket().

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));
       server.add_delim("\n");
       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(), Network::NetSocket::_port, and Network::Socket::_socket.

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;
   //NetworkUdpSocket            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));
       client.add_delim("\n");
       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(), Network::NetSocket::_port, and Network::Socket::_socket.


The documentation for this class was generated from the following files:
Generated on Thu Dec 28 19:14:03 2006 for libsocket by  doxygen 1.4.7