Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

NetworkListener.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 //
00014 // NetworkListener
00015 //
00016 // 23 March 2004 -- tds
00017 //
00018 
00019 #ifndef INDRI_NETWORKLISTENER_HPP
00020 #define INDRI_NETWORKLISTENER_HPP
00021 
00022 #include "lemur-compat.hpp"
00023 #include "indri/NetworkStream.hpp"
00024 
00025 class NetworkListener {
00026   socket_t _socket;
00027 
00028 public:
00029   NetworkListener() : _socket(-1) {}
00030   ~NetworkListener() {
00031     close();
00032   }
00033 
00034   bool listen( unsigned int port ) {
00035     lemur_compat::initializeNetwork();
00036     _socket = ::socket( AF_INET, SOCK_STREAM, 0 );
00037 
00038     sockaddr_in sa;
00039     sa.sin_addr.s_addr = INADDR_ANY;
00040     sa.sin_port = htons(port);
00041     sa.sin_family = AF_INET;
00042     memset( &sa.sin_zero, 0, sizeof sa.sin_zero );
00043 
00044     ::bind( _socket, (const sockaddr*) &sa, sizeof sa );
00045     int result = ::listen( _socket, 8 );
00046 
00047     if( result ) {
00048       close();
00049       return false;
00050     }
00051 
00052     return true;
00053   }
00054 
00055   NetworkStream* accept() {
00056     assert( _socket != -1 );
00057     socket_t s = ::accept( _socket, 0, 0 );
00058     return new NetworkStream(s);
00059   }
00060 
00061   void close() {
00062     lemur_compat::closesocket(_socket);
00063     _socket = -1;
00064   }
00065 };
00066 
00067 #endif // INDRI_NETWORKLISTENER_HPP

Generated on Wed Nov 3 12:59:00 2004 for Lemur Toolkit by doxygen1.2.18