49 iResult = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
58 SOCKET hsock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
70 sockaddr_in recvAddr{};
71 recvAddr.sin_family = AF_INET;
72 recvAddr.sin_port = htons(port);
73 if (::inet_pton(AF_INET, ipaddr.c_str(), &recvAddr.sin_addr.s_addr) <= 0)
80 unsigned long block = 1;
81 ::ioctlsocket(
static_cast<unsigned int>(
m_pSockRecord->socket()),
static_cast<int>(FIONBIO), &block);
102 long timeoutSeconds =
static_cast<long>(timeoutMs / 1000U);
103 long timeoutUSeconds =
static_cast<long>((timeoutMs % 1000U) * 1000U);
105 time_t timeoutSeconds =
static_cast<time_t
>(timeoutMs / 1000U);
106 suseconds_t timeoutUSeconds =
static_cast<time_t
>((timeoutMs % 1000U) * 1000U);
110 tv.tv_sec = timeoutSeconds;
111 tv.tv_usec = timeoutUSeconds;
117 if (::WSAGetLastError() != WSAEWOULDBLOCK)
124 if (
errno != EINPROGRESS)
136 int ret = ::select(
static_cast<int>(
m_pSockRecord->socket() + 1),
nullptr, &setW, &setE, &tv);
145 ::WSASetLastError(WSAETIMEDOUT);
164 socklen_t len =
sizeof(so_error);
165 ::getsockopt(
m_pSockRecord->socket(), SOL_SOCKET, SO_ERROR, &ret, &len);
178 unsigned long block = 0;
194 flags &= ~O_NONBLOCK;
208 iResult = ::setsockopt(
209 m_pSockRecord->socket(), SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*
>(&timeoutMs),
sizeof(DWORD));
211 iResult = ::setsockopt(
212 m_pSockRecord->socket(), SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const char*
>(&tv),
sizeof(
struct timeval));
232 const bufsize_t eff_buflen = castClamped<bufsize_t>(size);
234 return ::send(
m_pSockRecord->socket(), pData, eff_buflen, 0);
239 const bufsize_t eff_maxsize = castClamped<bufsize_t>(maxBytesToReceive);
242 buffer.resize(
static_cast<std::size_t
>(eff_maxsize));
243 char* pBuffer =
reinterpret_cast<char*
>(buffer.data());
249 buffer.resize(
static_cast<size_t>(retval));
260 buffer.resize(nBytesToReceive);
262 catch (std::length_error&)
266 if (nBytesToReceive < 1024u * 1024u * 125u)
268 std::cout <<
"TcpSocket::" << __FUNCTION__ <<
": Unable to allocate buffer of size " << nBytesToReceive
274 char*
const pBufferStart =
reinterpret_cast<char*
>(buffer.data());
275 char* pBuffer = pBufferStart;
277 while (nBytesToReceive > 0)
279 const bufsize_t eff_maxsize = castClamped<bufsize_t>(nBytesToReceive);
287 else if (bytesReceived == 0)
292 pBuffer += bytesReceived;
293 nBytesToReceive -=
static_cast<size_t>(bytesReceived);
296 buffer.resize(
static_cast<size_t>(pBuffer - pBufferStart));
305 int error_code_size =
sizeof(int);
310 std::cout <<
"Error getting error code" << std::endl;
313 socklen_t error_code_size =
sizeof error_code;
316 std::cout <<
"Error getting error code" << std::endl;