Logo Search packages:      
Sourcecode: libsocket version File versions  Download package

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

Connect as an TCP client.

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 >> 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(), and Network::NetSocket::_connect().

  {
    _port = port;
    _socket = _bind(port, hostname);
    _connect(_socket, port, hostname);
  }


Generated by  Doxygen 1.6.0   Back to index