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 #include "cxutils/networking/tcpclient.h"
00041 #include <string.h>
00042
00043 using namespace CxUtils;
00044
00050 TcpClient::TcpClient()
00051 {
00052 mPort = 0;
00053 mGoodSocket = false;
00054 mComputer = 0;
00055 mpHP = NULL;
00056 }
00057
00058
00064 TcpClient::~TcpClient()
00065 {
00066 Shutdown();
00067 mPort = 0;
00068 mGoodSocket = false;
00069 mComputer = 0;
00070 mpHP = NULL;
00071 }
00072
00073
00079 void TcpClient::Shutdown()
00080 {
00081 Socket::Shutdown();
00082 mGoodSocket = false;
00083 mPort = 0;
00084 this->mSocketType = Undefined;
00085 mpHP = NULL;
00086 }
00087
00088
00103 int TcpClient::InitializeSocket(const IP4Address& ipAddress,
00104 const unsigned short port,
00105 const int stimeout,
00106 const int rtimeout)
00107 {
00108 #ifdef WIN32
00109 SOCKET s;
00110 #else
00111 int s;
00112 #endif
00113 sockaddr_in service;
00114 hostent *hp;
00115 int slen;
00116 long computer;
00117
00118 #ifdef WIN32
00119 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00120 #else
00121 s = socket(PF_INET, SOCK_STREAM, 0);
00122 #endif
00123
00124 if(s > 0)
00125 {
00126
00127 memset(&service, 0, sizeof(struct sockaddr_in));
00128 slen = sizeof(service);
00129
00130 hp = gethostbyname(ipAddress.mString.c_str());
00131 if(hp != NULL)
00132 {
00133 computer = *((long *) hp->h_addr);
00134 service.sin_family = AF_INET;
00135 service.sin_port = htons(port);
00136 service.sin_addr.s_addr = computer;
00137
00138 #ifdef WIN32
00139 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO , (char *)&rtimeout, sizeof(rtimeout));
00140 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO , (char *)&stimeout, sizeof(stimeout));
00141 int nodelay = 1;
00142 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&nodelay, sizeof(int));
00143
00144 #else
00145 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &rtimeout, sizeof(rtimeout));
00146 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &stimeout, sizeof(stimeout));
00147 int nodelay = 1;
00148 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(int));
00149 #endif
00150 if(connect(s, (struct sockaddr *)&service, sizeof(struct sockaddr_in)) == 0)
00151 {
00152 this->mGoodSocket = true;
00153 this->mSocket = s;
00154 this->mService = service;
00155 this->mServiceLength = slen;
00156 this->mSocketType = TCPClient;
00157 this->mServerAddress = ipAddress;
00158 return 1;
00159 }
00160 }
00161
00162 #ifdef WIN32
00163 int e = WSAGetLastError();
00164 closesocket(s);
00165 #else
00166 close(s);
00167 #endif
00168 }
00169
00170 return 0;
00171 }
00172
00173
00184 int TcpClient::SendFromBuffer(const char* buffer, const unsigned int length) const
00185 {
00186 int result = 0;
00187 if(!mGoodSocket || mSocketType != TCPClient)
00188 {
00189 return 0;
00190 }
00191
00192 result = ::send(this->mSocket, buffer, ((int)(length)), 0);
00193
00194 if(result < 0)
00195 {
00196 result = 0;
00197 }
00198 return result;
00199 }
00200
00201
00217 int TcpClient::RecvToBuffer(char* buffer,
00218 const unsigned int length,
00219 const long int timeoutMilliseconds,
00220 IPAddress* ipAddress,
00221 unsigned short* port) const
00222 {
00223 int result;
00224
00225 if(!mGoodSocket || mSocketType != TCPClient)
00226 {
00227 return 0;
00228 }
00229
00230 if(!Socket::IsIncommingData(this, timeoutMilliseconds))
00231 {
00232 return 0;
00233 }
00234 result = ::recv(this->mSocket, buffer, ((int)(length)), 0);
00235
00236 if(result < 0)
00237 {
00238 result = 0;
00239 }
00240 else
00241 {
00242 if(ipAddress && dynamic_cast<IP4Address*>(ipAddress))
00243 {
00244 *((IP4Address*)(ipAddress)) = mServerAddress;
00245 }
00246 }
00247 return result;
00248 }
00249
00250