00001 /////////////////////////////////////////////////////////////////////////////////////////////// 00002 /*! \file ServerSocket.cpp 00003 * \brief Implementation of the ServerSocket class for communicating to a client program. 00004 * 00005 * The socket communications classes are based on the tutorial entitled 00006 * "Linux Socket Programming In C++" by Rob Tougher on the Linux Gazette 00007 * website. ( http://linuxgazette.net/issue74/tougher.html ) 00008 * 00009 * \author Scott A. Kowalchuk 00010 * \date 2006/11/29 00011 *///////////////////////////////////////////////////////////////////////////////////////////// 00012 /* 00013 * \Status 00014 * 00015 * 00016 */ 00017 ///////////////////////////////////////////////////////////////////////////////////////////// 00018 00019 00020 #include "ServerSocket.h" 00021 #include "SocketException.h" 00022 00023 /*! \brief Create server socket. 00024 * 00025 * @param port is the open port on the server 00026 * @return throw SocketException if error 00027 */ 00028 ServerSocket::ServerSocket ( int port ) 00029 { 00030 if ( ! Socket::create() ) 00031 { 00032 throw SocketException ( "Could not create server socket." ); 00033 } 00034 00035 if ( ! Socket::bind ( port ) ) 00036 { 00037 throw SocketException ( "Could not bind to port." ); 00038 } 00039 00040 if ( ! Socket::listen() ) 00041 { 00042 throw SocketException ( "Could not listen to socket." ); 00043 } 00044 00045 } 00046 00047 /*! \brief Destroy server socket. 00048 * 00049 */ 00050 ServerSocket::~ServerSocket() 00051 { 00052 } 00053 00054 /*! \brief Send string to client. 00055 * 00056 * @param str sting of data to send 00057 * @return throw SocketException if error 00058 */ 00059 const ServerSocket& ServerSocket::operator << ( const std::string& str ) const 00060 { 00061 if ( ! Socket::send ( str ) ) 00062 { 00063 throw SocketException ( "Could not write to socket." ); 00064 } 00065 00066 return *this; 00067 00068 } 00069 00070 /*! \brief Receive string from cleint. 00071 * 00072 * @param str sting of data to send 00073 * @return throw SocketException if error 00074 */ 00075 const ServerSocket& ServerSocket::operator >> ( std::string& str ) const 00076 { 00077 if ( ! Socket::recv ( str ) ) 00078 { 00079 throw SocketException ( "Could not read from socket." ); 00080 } 00081 00082 return *this; 00083 } 00084 00085 /*! \brief Accept server socket. 00086 * 00087 * @param sock 00088 */ 00089 void ServerSocket::accept ( ServerSocket& sock ) 00090 { 00091 if ( ! Socket::accept ( sock ) ) 00092 { 00093 throw SocketException ( "Could not accept socket." ); 00094 } 00095 } 00096