C++ Code:
int CheckSocket
(int sock
, long sec
, long usec
, bool Read
){ fd_set fds
= {0}; struct timeval tv
= {0}; FD_ZERO
(&fds
); FD_SET
(sock
, &fds
); tv.
tv_sec = sec
; tv.
tv_usec = usec
; return select
(sock
+ 1, Read
? &fds
: NULL
, Read
? NULL
: &fds
, NULL
, &tv
);}#if defined _WIN32 || defined _WIN64std
::string LastErrorMessage
(){ DWORD Error
= GetLastError
(); LPTSTR lpMsgBuf
= nullptr
; FormatMessageA
(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
, nullptr
, Error
, MAKELANGID
(LANG_NEUTRAL
, SUBLANG_DEFAULT
), reinterpret_cast
<LPTSTR
>(&lpMsgBuf
), 0, nullptr
); std
::string result
= std
::string(lpMsgBuf
); LocalFree
(lpMsgBuf
); return result
;}#elsestd
::string LastErrorMessage
(){ return std
::string(strerror(errno
));}#endif
Then call:
C++ Code:
for (int i = 0; i < 600; ++i)
{
int sel = CheckSocket(client->socket + 1, 0, 100000, true); //100ms time-out on read.
if (sel > 0) //If the socket is ready to be read and != SOCKET_ERROR aka -1.
{
if (recv(client->socket, (char*)&funid, sizeof(char), 0) != sizeof(char))
{
debug << "Call appears to have failed, or client successfully killed.\n";
debug << "Last Error: "<<LastErrorMessage()<<"\n"; //Print failure in proper format..
}
return;
}
}