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

void Network::LocalSocket::init ( const std::string &  filename  ) 

Initialize a local socket (server).

Here is an example of named server server using libsocket :

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

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

   if (argc < 2)
     {
       std::cout << "Use: " << argv[0] << " filename" << std::endl;
       exit(0);
     }
   try
     {
       filename = std::string(argv[1]);
       server.init(filename);
       while (str != "quit")
 	{
 	  //server.read(filename 30); //read with a timeout of 30 seconds
 	  //server >> str; //read without geting the named name
 	  //(cannot sent data)
 	  str = server.read(client);
 	  std::string msg = "ok, I received [" + str + "]";
 	  server.writeto(msg, client);
 	  std::cout << "[" << str << "] from : " << client << 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);
     }
 }

 
Here is an example of named server server using libsocket :
     include <stdlib.h>
 include <iostream>
 include <string>
 include "socket/localsocket.hh"

 int main(int argc, char **argv)
 {
   Network::LocalSocket		client;
   std::string			client_filename, server_filename, str(""), msg;

   if (argc < 3)
     {
       std::cout << "Use: " << argv[0] << " <client_filename> " 
 		<< "<server_filename>" << std::endl;
       exit(0);
     }
   try
     {
       client_filename = std::string(argv[1]);
       server_filename = std::string(argv[2]);

       client.init(client_filename);
       while (str != "quit")
 	{
 	  std::cout << "Msg: ";
 	  std::cin >> str;
 	  client.writeto(str, server_filename);
 	  client >> msg;
 	  std::cout << "[received] " << msg << std::endl;
 	}
       client.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 localsocket.cc.

References _bind().

  {
    _socket = _bind(filename);
    _filename = filename;
  }


Generated by  Doxygen 1.6.0   Back to index