Here's some WinSock code to set time-out
Here's some sample code to set the time-out
// First create a socket.
void CSocketClass::Create(int nType /* = SOCK_STREAM */)
{
ASSERT(m_hSocket == NULL);
if((m_hSocket = socket(AF_INET, nType, 0)) == INVALID_SOCKET)
{
throw new YourException
}
}
// Set's time-out for the socket.
void CSocketClass::SetTimeout(int nTimeout ) // in seconds.
{
// Set the socket timeout for reply messages
m_nTimeout = nTimeout;
nTimeout *= 1000; // convert to milliseconds.
if( setsockopt(m_hSocket, SOL_SOCKET, SO_RCVTIMEO,
(char *) &nTimeout, sizeof( int ) ) == SOCKET_ERROR) {
throw new YourException()
}
return;
}
Re: Here's some WinSock code to set time-out
Hi.
It doesn't work.
If you try SDK sample, ping test. It becomes blocked if there's nothing to receive.
Dima.
Re: Here's some WinSock code to set time-out
The code won't work because winsock documentation itself says that it is not supporting any timeout option with receive or send.
I even tried attaching a window with my socket using class and using a window timer event. but everything is on hold when i am in blocking receive method.
in case you have that code working in some example and send the example that would be useful.
Thanks for replying.
Samir
Re: Time out in MFC Sockets
There's better way to time out sockets.
Use function select, where you can set timeout and what you expect from socket, here's some code:
int CSSocket::WaitForData(int timeout) // pure timeout for socket
{
FDSET fd;
fd.count = 1;
fd.sockarr[0] = m_Socket;
TIMEVAL tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = timeout*1000 - tv.tv_sec * 1000000;
return select(0, (FD_SET*)&fd, NULL, (FD_SET*)&fd, &tv);
}
It returns either timeout, socket_error or somedata wait for being receiving
Hope it'l help