00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "cxutils/networking/tcplistensocket.h"
00043 #include "cxutils/networking/tcpserver.h"
00044 #include "cxutils/networking/socket.h"
00045 #include <string.h>
00046 #include <iostream>
00047
00048 using namespace std;
00049
00050 #ifdef WIN32
00051 unsigned int CxUtils::TcpListenSocket::mWinsockInitCount = 0;
00052 #else
00053 #include <netinet/tcp.h>
00054 #endif
00055
00056
00057 using namespace CxUtils;
00058
00064 TcpListenSocket::TcpListenSocket()
00065 {
00066 mSocket = 0;
00067 mServiceLength = 0;
00068 memset(&mService, 0, sizeof(struct sockaddr_in));
00069 mPort = 0;
00070 mGoodSocket = false;
00071 mComputer = 0;
00072 mpHP = NULL;
00073 mNetworkInterface = -1;
00074 #ifdef WIN32
00075
00076
00077 if(mWinsockInitCount == 0)
00078 {
00079 WSADATA wsaData;
00080
00081 WSAStartup(MAKEWORD(2,2), &wsaData);
00082
00083
00084 mWinsockInitCount++;
00085 }
00086 else
00087 {
00088 mWinsockInitCount++;
00089 }
00090 #endif
00091 }
00092
00093
00099 TcpListenSocket::~TcpListenSocket()
00100 {
00101 Shutdown();
00102 #ifdef WIN32
00103 mWinsockInitCount--;
00104 if(mWinsockInitCount == 0)
00105 {
00106
00107
00108
00109 WSACleanup();
00110 }
00111 #endif
00112 }
00113
00114
00126 bool TcpListenSocket::SetNetworkInterface(const int num)
00127 {
00128 mNetworkInterface = num;
00129 return true;
00130 }
00131
00132
00143 bool TcpListenSocket::SetNetworkInterface(const IP4Address& ipAddress)
00144 {
00145 int num = Socket::GetNetworkInterface(ipAddress);
00146 if(num >= 0)
00147 {
00148 return SetNetworkInterface(num);
00149 }
00150 return false;
00151 }
00152
00153
00171 int TcpListenSocket::InitializeSocket(const unsigned short port,
00172 const unsigned int queuesize,
00173 const unsigned int stimeout,
00174 const unsigned int rtimeout,
00175 const int nonblocking)
00176 {
00177 #ifdef WIN32
00178 SOCKET listenSocket;
00179 #else
00180 int listenSocket;
00181 #endif
00182 int result = 0;
00183
00184
00185 Shutdown();
00186
00187 listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00188
00189 if(listenSocket > 0)
00190 {
00191
00192 if(nonblocking > 0)
00193 {
00194 #ifdef WIN32
00195 u_long noblock = 1;
00196 if(ioctlsocket(listenSocket, FIONBIO, &noblock) == SOCKET_ERROR)
00197 {
00198 mGoodSocket = false;
00199 return 0;
00200 }
00201 #else
00202 unsigned int noblock = 1;
00203 if(ioctl(listenSocket, FIONBIO, &noblock) == -1)
00204 {
00205 cout << "Non Blocking Failure!\n";
00206 mGoodSocket = false;
00207 return 0;
00208 }
00209 #endif
00210 }
00211
00212
00213
00214 mPort = port;
00215 memset(&this->mService, 0, sizeof(struct sockaddr_in) );
00216 this->mServiceLength = sizeof(mService);
00217
00218 this->mService.sin_family = AF_INET;
00219 this->mService.sin_port = htons(mPort);
00220 this->mService.sin_addr.s_addr = htonl(INADDR_ANY);
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 if( bind(listenSocket, (struct sockaddr *)& this->mService, sizeof(mService)) >= 0 )
00235 {
00236
00237 if(listen(listenSocket, queuesize) < 0)
00238 {
00239
00240
00241 #ifdef WIN32
00242 closesocket(listenSocket);
00243 #else
00244 close(listenSocket);
00245 #endif
00246
00247 Shutdown();
00248 result = false;
00249 return result;
00250 }
00251 else
00252 {
00253
00254
00255 #ifdef WIN32
00256 setsockopt(listenSocket, SOL_SOCKET, SO_RCVTIMEO , (char *)&rtimeout, sizeof(int));
00257 setsockopt(listenSocket, SOL_SOCKET, SO_SNDTIMEO , (char *)&stimeout, sizeof(int));
00258 int nodelay = 1;
00259 setsockopt(listenSocket, IPPROTO_TCP, TCP_NODELAY, (char *)&nodelay, sizeof(int));
00260 #else
00261 setsockopt(listenSocket, SOL_SOCKET, SO_RCVTIMEO, &rtimeout, sizeof(int));
00262 setsockopt(listenSocket, SOL_SOCKET, SO_SNDTIMEO, &stimeout, sizeof(int));
00263 int nodelay = 1;
00264 setsockopt(listenSocket, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(int));
00265 #endif
00266
00267
00268
00269 memcpy(&this->mSocket, &listenSocket, sizeof(int));
00270 result = 1;
00271 }
00272 }
00273 }
00274
00275 #ifdef WIN32
00276 int value;
00277
00278 value = WSAGetLastError();
00279 #endif
00280
00281
00282
00283
00284 if(!result)
00285 {
00286 Shutdown();
00287 mGoodSocket = false;
00288 }
00289 else
00290 {
00291 mGoodSocket = true;
00292 }
00293
00294 return result;
00295 }
00296
00297
00303 void TcpListenSocket::Shutdown()
00304 {
00305 #ifdef WIN32
00306 shutdown(mSocket, 1);
00307 closesocket(mSocket);
00308 #else
00309 if(mSocket > 0)
00310 {
00311 if(shutdown(mSocket, 1) == 0)
00312 {
00313 mSocket = 0;
00314 }
00315 }
00316 if(mSocket > 0)
00317 {
00318 close(mSocket);
00319 }
00320 #endif
00321 mSocket = 0;
00322 this->mGoodSocket = false;
00323 mServiceLength = 0;
00324 memset(&mService, 0, sizeof(struct sockaddr_in));
00325 mPort = 0;
00326 mpHP = NULL;
00327 mComputer = 0;
00328 }
00329
00343 int TcpListenSocket::AwaitConnection(TcpServer& socket) const
00344 {
00345 int tSocket;
00346
00347 int tSendAddrLength = sizeof(struct sockaddr_in);
00348 struct sockaddr tSendAddr;
00349
00350 socket.Shutdown();
00351
00352
00353
00354 if( !IsValid() )
00355 return 0;
00356
00357 tSocket = -1;
00358
00359
00360 #ifdef WIN32
00361 tSocket = (int)accept(mSocket,
00362 (struct sockaddr *) &tSendAddr,
00363 &tSendAddrLength);
00364 #else
00365 tSocket = accept(mSocket,
00366 (struct sockaddr *) &tSendAddr,
00367 (socklen_t *)&tSendAddrLength);
00368 #endif
00369
00370 if(tSocket == -1)
00371 return 0;
00372
00373 socket.mGoodSocket = true;
00374 socket.mSocket = tSocket;
00375 socket.mService = this->mService;
00376 socket.mServiceLength = this->mServiceLength;
00377 socket.mSocketType = Socket::TCPServer;
00378 struct sockaddr_in inSocket;
00379 memcpy(&inSocket, &tSendAddr, sizeof(struct sockaddr_in));
00380 socket.mClientAddress = std::string(inet_ntoa(inSocket.sin_addr));
00381
00382 return 1;
00383 }
00384
00399 TcpServer *TcpListenSocket::AwaitConnection() const
00400 {
00401 int tSocket;
00402
00403 int tSendAddrLength = sizeof(struct sockaddr_in);
00404 struct sockaddr tSendAddr;
00405
00406
00407
00408 if( !IsValid() )
00409 return NULL;
00410
00411 tSocket = -1;
00412
00413
00414 #ifdef WIN32
00415 tSocket = (int)accept(mSocket,
00416 (struct sockaddr *) &tSendAddr,
00417 &tSendAddrLength);
00418 #else
00419 tSocket = accept(mSocket,
00420 (struct sockaddr *) &tSendAddr,
00421 (socklen_t *)&tSendAddrLength);
00422 #endif
00423
00424 if(tSocket == -1)
00425 return NULL;
00426
00427 TcpServer *newSocket = new TcpServer();
00428 newSocket->mGoodSocket = true;
00429 newSocket->mSocket = tSocket;
00430 newSocket->mService = this->mService;
00431 newSocket->mServiceLength = this->mServiceLength;
00432 newSocket->mSocketType = Socket::TCPServer;
00433 struct sockaddr_in inSocket;
00434 memcpy(&inSocket, &tSendAddr, sizeof(struct sockaddr_in));
00435 newSocket->mClientAddress = std::string(inet_ntoa(inSocket.sin_addr));
00436
00437 return newSocket;
00438 }
00439
00440