00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "Socket.h"
00021
00022
00023
00024
00025 Socket::Socket( )
00026 : m_sock ( -1 )
00027 {
00028 memset ( &m_addr, 0, sizeof( m_addr ) );
00029 }
00030
00031
00032
00033
00034
00035 Socket::~Socket( )
00036 {
00037 if ( is_valid( ) )
00038 {
00039 ::close ( m_sock );
00040 }
00041 }
00042
00043
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
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
00065
00066
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
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
00113
00114
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
00133
00134
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
00151
00152
00153
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
00183
00184
00185
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
00215
00216
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