Network::TcpSocket Class Reference

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

#include <tcpsocket.hh>

Inheritance diagram for Network::TcpSocket:

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

Public Member Functions

 TcpSocket (SOCKET_VERSION version=V4)
 TcpSocket (PROTO_KIND pkind, SOCKET_VERSION version=V4)
 TcpSocket (int socket, SOCKET_VERSION version=V4)
 TcpSocket (int socket, PROTO_KIND pkind, SOCKET_VERSION version=V4)
virtual ~TcpSocket ()
void connect (const std::string &hostname, int port)
 Here is an example of tcp client using libsocket :.
std::string get_ip (TcpSocket *client) const
 return ip of client (after an accept)
TcpSocketaccept () const
 accept a new client (For server only)
void connect (int port)
 Here is an example of tcp 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 psize)
 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 psize)
 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 tcp connection (client and server).

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

Definition at line 32 of file tcpsocket.hh.


Constructor & Destructor Documentation

Network::TcpSocket::TcpSocket ( SOCKET_VERSION  version = V4  )  [inline]

Definition at line 35 of file tcpsocket.hh.

Referenced by accept().

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

Definition at line 38 of file tcpsocket.hh.

Network::TcpSocket::TcpSocket ( int  socket,
SOCKET_VERSION  version = V4 
) [inline]

Definition at line 41 of file tcpsocket.hh.

References Network::Socket::_socket.

Network::TcpSocket::TcpSocket ( int  socket,
PROTO_KIND  pkind,
SOCKET_VERSION  version = V4 
) [inline]

Definition at line 46 of file tcpsocket.hh.

References Network::Socket::_socket.

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

Definition at line 52 of file tcpsocket.hh.

References close().


Member Function Documentation

std::string Network::TcpSocket::_read_line_bin ( int  socket,
unsigned int  psize 
) [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 81 of file tcpsocket.cc.

References Network::Socket::_buffer, Network::Socket::_tls, HERE, and Network::NetSocket::read().

std::string Network::TcpSocket::_read_line_bin ( int  socket,
int &  port,
std::string &  host,
unsigned int  psize 
) [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 153 of file tcpsocket.cc.

References Network::Socket::_buffer, Network::Socket::_tls, Network::Socket::_version, HERE, Network::NetSocket::read(), and Network::V4.

TcpSocket * Network::TcpSocket::accept (  )  const

accept a new client (For server only)

Definition at line 42 of file tcpsocket.cc.

References Network::NetSocket::_accept(), Network::NetSocket::_port, Network::Socket::_proto_kind, Network::Socket::_socket, Network::Socket::_tls, Network::Socket::_version, Network::Socket::enable_tls(), TcpSocket(), Network::V4, and Network::V6.

void Network::TcpSocket::close (  ) 

Close the connection.

Definition at line 74 of file tcpsocket.cc.

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

Referenced by ~TcpSocket().

void Network::TcpSocket::connect ( int  port  ) 

Here is an example of tcp server using libsocket :.

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

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

   if (argc < 2)
     {
       std::cout << "Use: " << argv[0] << " port" << std::endl;
       exit(0);
     }
   try
     {
       std::cout << "--- echo server ---" << std::endl;
       server.connect(strtol(argv[1], NULL, 10));
       server.add_delim("\n");
       client = server.accept();
       client->add_delim("\n");
       (*client) << "Welcome on test server";
       while (str != "quit")
 	       {

 	         //(*client) >> str;
 	         // read with a timeout of 30 second and get port and host
 	         str = client->read(port, host, 30);
 	         std::cout << "[" << str << "] from [" << host << ":"
 		           << port << "]" << std::endl;
 	         (*client) << str;
 	       }
       server.close();
       exit (0);
     }
   catch (Network::Exception e) // catch all libsocket errors
     {
       std::cerr << e;
       exit(1);
     }
 }
 

Definition at line 35 of file tcpsocket.cc.

References Network::NetSocket::_bind(), Network::Socket::_listen(), Network::NetSocket::_port, and Network::Socket::_socket.

void Network::TcpSocket::connect ( const std::string &  hostname,
int  port 
)

Here is an example of tcp client using libsocket :.

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

 int main(int argc, char **argv)
 {
   Network::TcpSocket            client;
   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");
       client >> str;
       std::cout << str << std::endl;

       while (str != "quit")
         {
           std::cin >> str;
           client << str;
           client >> str;
         }
       client.close();
       exit (0);
     }
   catch (Network::Exception e) // catch all libsocket errors
     {
       std::cerr << e;
       exit(1);
     }

 }
 

Definition at line 28 of file tcpsocket.cc.

References Network::NetSocket::_bind(), Network::NetSocket::_connect(), Network::NetSocket::_port, and Network::Socket::_socket.

std::string Network::TcpSocket::get_ip ( TcpSocket client  )  const

return ip of client (after an accept)

Definition at line 69 of file tcpsocket.cc.

References Network::NetSocket::_get_ip(), 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