Go to the documentation of this file.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 #include "DcSocket.h"
00026
00027 WSADATA CdcSocket::oData;
00028
00029 int CdcSocket::nReqMaxLength = 0;
00030 int CdcSocket::nWaitReqTime = 0;
00031
00032
00033 CdcSocket::CdcSocket () throw (CdcException)
00034 {
00035
00036 oSocket = INVALID_SOCKET;
00037 oClient.sin_port = 0;
00038 oClient.sin_addr.S_un.S_addr = 0;
00039 StartNewRequest ();
00040 aBuffer = new char [nReqMaxLength];
00041 if (aBuffer == NULL)
00042 throw CdcException ("Cannot allocate memory");
00043 }
00044
00045
00046
00047 CdcSocket::~CdcSocket ()
00048 {
00049
00050 closesocket (oSocket);
00051
00052 if (aBuffer != NULL)
00053 delete[] aBuffer;
00054 }
00055
00056
00057
00058 CdcSocket* CdcSocket::GetNewSocket (SOCKET soc, SOCKADDR_IN from) throw (CdcException)
00059 {
00060 CdcSocket* psock = new CdcSocket;
00061 psock->Init (soc, from);
00062 return psock;
00063 }
00064
00065
00066 void CdcSocket::Init (SOCKET soc, SOCKADDR_IN from) throw (CdcException)
00067 {
00068
00069 Init (soc);
00070
00071 oClient = from;
00072 }
00073
00074 void CdcSocket::Init (SOCKET soc) throw (CdcException)
00075 {
00076
00077 if (soc == INVALID_SOCKET)
00078 throw CdcExceptionSocket (WSAGetLastError());
00079
00080
00081 oSocket = soc;
00082
00083
00084 int ret = setsockopt (oSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&nWaitReqTime,
00085 sizeof (nWaitReqTime));
00086
00087
00088 if (ret == SOCKET_ERROR)
00089 throw CdcExceptionSocket (WSAGetLastError());
00090 }
00091
00092
00093 int CdcSocket::Read (char* buff, int lng) throw (CdcException)
00094 {
00095
00096 int ret = recv (oSocket, buff, lng, 0);
00097 nReqLength += ret;
00098
00099
00100 if (nReqLength >= nReqMaxLength)
00101 throw CdcExceptionSocket (WSAEMSGSIZE);
00102
00103 if (ret == SOCKET_ERROR)
00104 throw CdcExceptionSocket (WSAGetLastError());
00105
00106 if (ret == 0)
00107 throw CdcExceptionSocket (WSAECONNRESET);
00108 return ret;
00109 }
00110
00111
00112 int CdcSocket::Read (CdcString& msg) throw (CdcException)
00113 {
00114 int ret = Read (aBuffer, nReqMaxLength - 1);
00115 aBuffer[ret] = '\0';
00116 msg += aBuffer;
00117 return ret;
00118 }
00119
00120
00121 int CdcSocket::Write (const char* buff, int lng) throw (CdcException)
00122 {
00123
00124 int ret = send (oSocket, buff, lng, 0);
00125
00126
00127 if (ret == SOCKET_ERROR)
00128 throw CdcExceptionSocket (WSAGetLastError());
00129
00130 return ret;
00131 }
00132
00133
00134 const CdcString& CdcSocket::GetClientAddress ()
00135 {
00136 if (sClientIP.IsEmpty () == false) return sClientIP;
00137 if (oClient.sin_addr.S_un.S_addr == 0) return EMPTY_STR;
00138
00139 IN_ADDR& adr = oClient.sin_addr;
00140
00141 sClientIP = CdcString::IntToString (adr.S_un.S_un_b.s_b1) + '.' +
00142 CdcString::IntToString (adr.S_un.S_un_b.s_b2) + '.' +
00143 CdcString::IntToString (adr.S_un.S_un_b.s_b3) + '.' +
00144 CdcString::IntToString (adr.S_un.S_un_b.s_b4);
00145
00146 return sClientIP;
00147 }
00148
00149
00150 int CdcSocket::GetClientPort () const
00151 {
00152 return oClient.sin_port;
00153 }
00154
00155
00156 void CdcSocket::Startup (int max_req_len, int wait_req_time)
00157 throw (CdcException)
00158 {
00159 nReqMaxLength = max_req_len;
00160 nWaitReqTime = wait_req_time;
00161
00162
00163 int ret = WSAStartup (0x202, &oData);
00164
00165
00166 if (ret == SOCKET_ERROR)
00167 throw CdcExceptionSocket (WSAGetLastError());
00168 }
00169
00170
00171 void CdcSocket::Cleanup () throw (CdcException)
00172 {
00173
00174 int ret = WSACleanup();
00175
00176
00177 if (ret == SOCKET_ERROR)
00178 throw CdcExceptionSocket (WSAGetLastError());
00179 }