Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

Socket.cpp

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file       ClientSocket.cpp
00003  *  \brief      Implementation of the Socket class for communicating.
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 "Socket.h"
00021 
00022 /*! \brief Constructor of the socket
00023  * 
00024  */
00025 Socket::Socket( ) 
00026         : m_sock ( -1 )
00027 {
00028         memset ( &m_addr, 0, sizeof( m_addr ) );
00029 }
00030 
00031 
00032 /*! \brief Destructor of the socket
00033  * 
00034  */
00035 Socket::~Socket( )
00036 {
00037         if ( is_valid( ) ) 
00038         {
00039                 ::close ( m_sock );
00040         }
00041 }
00042 
00043 /*! \brief Create socket
00044  * 
00045  */
00046 bool Socket::create( )
00047 {
00048         m_sock = socket ( AF_INET, SOCK_STREAM, 0 );
00049 
00050         if ( ! is_valid() ) 
00051         {
00052                 return false;
00053         }
00054 
00055         /* TIME_WAIT */
00056         int on = 1;
00057         if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )
00058                 return false;
00059 
00060         return true;
00061 }
00062 
00063 
00064 /*! \brief Bind socket
00065  * 
00066  * @param port is the port to communicate with.
00067  */
00068 bool Socket::bind ( const int port )
00069 {
00070 
00071         if ( ! is_valid() )
00072         {
00073                 return false;
00074         }
00075 
00076         m_addr.sin_family = AF_INET;
00077         m_addr.sin_addr.s_addr = INADDR_ANY;
00078         m_addr.sin_port = htons ( port );
00079 
00080         int bind_return = ::bind ( m_sock, ( struct sockaddr * ) &m_addr, sizeof ( m_addr ) );
00081 
00082         if ( bind_return == -1 )
00083         {
00084                 return false;
00085         }
00086 
00087         return true;
00088 }
00089 
00090 
00091 /*! \brief Listen to socket
00092  * 
00093  */
00094 bool Socket::listen( ) const
00095 {
00096         if ( ! is_valid() )
00097         {
00098                 return false;
00099         }
00100 
00101         int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
00102 
00103         if ( listen_return == -1 )
00104         {
00105                 return false;
00106         }
00107 
00108         return true;
00109 }
00110 
00111 
00112 /*! \brief Accept socket
00113  * 
00114  * @param new_socket
00115  */
00116 bool Socket::accept ( Socket& new_socket ) const
00117 {
00118         int addr_length = sizeof ( m_addr );
00119         new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
00120 
00121         if ( new_socket.m_sock <= 0 )
00122         {
00123                 return false;
00124         }
00125         else
00126         {
00127                 return true;
00128         }
00129 }
00130 
00131 
00132 /*! \brief Send string to socket
00133  * 
00134  * @param str is the string of data to be sent
00135  */
00136 bool Socket::send ( const std::string str ) const
00137 {
00138         int status = ::send ( m_sock, str.c_str(), str.size(), MSG_NOSIGNAL );
00139         if ( status == -1 )
00140         {
00141                 return false;
00142         }
00143         else
00144         {
00145                 return true;
00146         }
00147 }
00148 
00149 
00150 /*! \brief Receive string from socket
00151  * 
00152  * @param str is the string of data to be received
00153  * @return status
00154  */
00155 int Socket::recv ( std::string& str ) const
00156 {
00157         char buf [ MAXRECV + 1 ];
00158 
00159         str = "";
00160 
00161         memset ( buf, 0, MAXRECV + 1 );
00162 
00163         int status = ::recv ( m_sock, buf, MAXRECV, 0 );
00164 
00165         if ( status == -1 )
00166         {
00167                 std::cout << "status == -1   errno == " << errno << "  in Socket::recv\n";
00168                 return 0;
00169         }
00170         else if ( status == 0 )
00171         {
00172                 return 0;
00173         }
00174         else
00175         {
00176                 str = buf;
00177                 return status;
00178         }
00179 }
00180 
00181 
00182 /*! \brief Connect to the host.
00183  * 
00184  * @param host is the IP address (xxx.xxx.xxx.xxx) or host name (vt.edu)
00185  * @param port is the port number for the host
00186  */
00187 bool Socket::connect ( const std::string host, const int port )
00188 {
00189         if ( ! is_valid() ) return false;
00190 
00191         m_addr.sin_family = AF_INET;
00192         
00193         hostent * hp;
00194         if( ( hp = gethostbyname( host.c_str() ) ) == NULL )
00195         {
00196                 std::cerr << "Can't Resolve Host" << std::endl;
00197                 return false;
00198         }
00199 
00200         m_addr.sin_addr.s_addr=*(u_long *)hp->h_addr;
00201         
00202         m_addr.sin_port = htons ( port );
00203 
00204         if ( errno == EAFNOSUPPORT ) return false;
00205 
00206         int status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );
00207 
00208         if ( status == 0 )
00209                 return true;
00210         else
00211                 return false;
00212 }
00213 
00214 /*! \brief Set non blocking.
00215  * 
00216  * @param b
00217  */
00218 void Socket::set_non_blocking( const bool b )
00219 {
00220         int opts;
00221 
00222         opts = fcntl ( m_sock, F_GETFL );
00223 
00224         if ( opts < 0 )
00225         {
00226                 return;
00227         }
00228 
00229         if ( b )
00230         {
00231                 opts = ( opts | O_NONBLOCK );
00232         }
00233         else
00234         {
00235                 opts = ( opts & ~O_NONBLOCK );
00236         }
00237 
00238         fcntl ( m_sock, F_SETFL,opts );
00239 
00240 }
00241 

Generated on Wed Sep 5 12:54:25 2007 for DSACSS Operational Code by  doxygen 1.3.9.1