19 #ifndef __SOCKET_H__BY_SGCHOI
20 #define __SOCKET_H__BY_SGCHOI
27 m_hSock = INVALID_SOCKET;
37 int bOptLen =
sizeof(BOOL);
40 static BOOL s_bInit = FALSE;
43 WORD wVersionRequested;
46 wVersionRequested = MAKEWORD(2, 0);
47 WSAStartup(wVersionRequested, &wsaData);
54 success = (m_hSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET;
61 if (m_hSock == INVALID_SOCKET)
65 shutdown(m_hSock, SD_SEND);
68 shutdown(m_hSock, SHUT_WR);
72 m_hSock = INVALID_SOCKET;
80 m_hSock = INVALID_SOCKET;
86 UINT addr_len =
sizeof(addr);
88 if (getsockname(m_hSock, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0)
90 return inet_ntoa(addr.sin_addr);
95 UINT addr_len =
sizeof(addr);
97 if (getsockname(m_hSock, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0)
99 return ntohs(addr.sin_port);
102 BOOL Bind(USHORT nPort = 0,
string ip =
"") {
104 sockaddr_in sockAddr;
105 memset(&sockAddr, 0,
sizeof(sockAddr));
106 sockAddr.sin_family = AF_INET;
110 setsockopt(m_hSock, SOL_SOCKET, SO_REUSEADDR, (
const char*) &on,
sizeof(on));
112 sockAddr.sin_addr.s_addr = inet_addr(ip.c_str());
114 if (sockAddr.sin_addr.s_addr == INADDR_NONE) {
116 phost = gethostbyname(ip.c_str());
118 sockAddr.sin_addr.s_addr = ((in_addr*) phost->h_addr)->s_addr;
125 sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
128 sockAddr.sin_port = htons(nPort);
130 return bind(m_hSock, (sockaddr *) &sockAddr,
sizeof(sockaddr_in)) >= 0;
133 BOOL Listen(
int nQLen = 5) {
134 return listen(m_hSock, nQLen) >= 0;
138 sock.m_hSock = accept(m_hSock, NULL, 0);
139 if (sock.m_hSock == INVALID_SOCKET)
145 BOOL Connect(
string ip, USHORT port, LONG lTOSMilisec = -1) {
146 sockaddr_in sockAddr;
147 memset(&sockAddr, 0,
sizeof(sockAddr));
148 sockAddr.sin_family = AF_INET;
149 sockAddr.sin_addr.s_addr = inet_addr(ip.c_str());
151 if (sockAddr.sin_addr.s_addr == INADDR_NONE) {
153 lphost = gethostbyname(ip.c_str());
155 sockAddr.sin_addr.s_addr = ((in_addr*) lphost->h_addr)->s_addr;
160 sockAddr.sin_port = htons(port);
166 if( lTOSMilisec > 0 )
168 setsockopt(m_hSock, SOL_SOCKET, SO_RCVTIMEO, (
char*) &lTOSMilisec,
sizeof(lTOSMilisec));
171 int ret = connect(m_hSock, (sockaddr*)&sockAddr,
sizeof(sockAddr));
173 if( ret >= 0 && lTOSMilisec > 0 )
174 setsockopt(m_hSock, SOL_SOCKET, SO_RCVTIMEO, (
char*) &dw,
sizeof(dw));
181 if (lTOSMilisec > 0) {
182 tv.tv_sec = lTOSMilisec / 1000;
183 tv.tv_usec = (lTOSMilisec % 1000) * 1000;
185 setsockopt(m_hSock, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
188 int ret = connect(m_hSock, (sockaddr*) &sockAddr,
sizeof(sockAddr));
190 if (ret >= 0 && lTOSMilisec > 0) {
194 setsockopt(m_hSock, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
201 uint64_t Receive(
void* pBuf, uint64_t nLen,
int nFlags = 0) {
202 char* p = (
char*) pBuf;
206 ret = recv(m_hSock, p, n, 0);
214 if ( errno == EAGAIN) {
215 cerr <<
"socket recv eror: EAGAIN" << endl;
219 cerr <<
"socket recv error: " << errno << endl;
220 perror(
"Socket error ");
223 }
else if (ret == 0) {
234 int Send(
const void* pBuf, uint64_t nLen,
int nFlags = 0) {
235 return send(m_hSock, (
char*) pBuf, nLen, nFlags);
242 #endif //SOCKET_H__BY_SGCHOI