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

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

Connect as an UDP server.

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().

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


Generated by  Doxygen 1.6.0   Back to index