The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Ghirai on May 04, 2007, 11:24:03 AM

Title: set wsock connect() timeout
Post by: Ghirai on May 04, 2007, 11:24:03 AM
Is there any way i can set a timeout for connect(), without using SetTimer (because i don't have any windows)?
Title: Re: set wsock connect() timeout
Post by: Tedd on May 04, 2007, 12:19:17 PM
The connection attempt will eventually timeout itself, but I don't see a way to control how long this should be - ultimately it's down to winsock, but depends on network conditions and whether any reply at all is received from the host, etc.

Create a separate thread and have it check the time passed?
Title: Re: set wsock connect() timeout
Post by: ecube on May 04, 2007, 06:59:38 PM
found in some source


TimeOut proto :DWORD,:DWORD
SO_RCVTIMEO         equ 1006h
SO_SNDTIMEO         equ 1005h
.data
IOTimeOut                   dd  0

.code
TimeOut         PROC    sock:DWORD, milliseconds:DWORD
        ;
        ; Set the timeout for sending and recieving data
        ;
        mov     eax, milliseconds
        mov     IOTimeOut, eax
        invoke    setsockopt, sock, SOL_SOCKET, SO_RCVTIMEO, addr IOTimeOut, 4 ;recv
        invoke    setsockopt, sock, SOL_SOCKET, SO_SNDTIMEO, addr IOTimeOut, 4 ;send
        ret
TimeOut         ENDP
Title: Re: set wsock connect() timeout
Post by: Ghirai on May 04, 2007, 08:27:30 PM
Thanks :bg
Title: Re: set wsock connect() timeout
Post by: Tedd on May 05, 2007, 02:17:28 PM
I did see those, but..

Quote
BSD options not supported for setsockopt are shown in the following table.

SO_ACCEPTCONN  BOOL  Socket is listening.
SO_RCVLOWAT     int      Receives low watermark.
SO_RCVTIMEO      int      Receives time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2).
SO_SNDLOWAT    int      Sends low watermark.
SO_SNDTIMEO     int       Sends time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2).
SO_TYPE             int       Type of the socket.

So make sure you specify version 2 in WSAStartup :wink
Also, be careful fiddling with those values - they will affect all sending and receiving (which includes all of the extra tcp packets to keep the connection open, etc) - so you might want to get the values first and then replace them upon connection. Also, due to the send-receive-send cycle of tcp connection negotiation, you can only effect the connection timeout in a roundabout way, it still depends on retries and acknowledgements, and ...... ::)

Title: Re: set wsock connect() timeout
Post by: Tedd on May 16, 2007, 11:24:59 AM
Just found SetWaitableTimer - which is much better than SetTimer (IMHO) and you don't need a window as it uses events not messages.
Might be a better solution than playing with critical timings?