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

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

Connect as an TCP server (echo server).

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));
       client = server.accept();
       (*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(), and Network::Socket::_listen().

  {
    _port = port;
    _socket = _bind(port);
    _listen(_socket);
  }


Generated by  Doxygen 1.6.0   Back to index