How to sets recv socket timeout?

How to sets recv socket timeout?

Post by Avramenko Dmitr » Tue, 05 Dec 2000 04:00:00



Please, can anybody give me a part of code, where
I can see how to set timeout for opened socket ?

Dmitry Avramenko.

 
 
 

How to sets recv socket timeout?

Post by David Pete » Tue, 05 Dec 2000 04:00:00


You usually can't on Unix systems. Use select(2) instead.

> Please, can anybody give me a part of code, where
> I can see how to set timeout for opened socket ?

> Dmitry Avramenko.



 
 
 

How to sets recv socket timeout?

Post by David Pete » Tue, 05 Dec 2000 04:00:00


You'll probably want sothing like this (e.g. to get 2.5 second timeout).

fd_set readfds;
struct timeval timeout;

int s = socket(...), n;

FD_ZERO(&readfds);
FD_SET(s, &readfds);

timeout.tv_sec = 2;
timeout.tv_usec = 500000;

n = select(s, &readfds, NULL, NULL, &timeout);
if (n == 0) {
        // timed out

Quote:} else if {n < 0) }

        // error, probably interrupt. test for EINTR in errno
Quote:} else if (FD_ISSET(s, &readfds) {

        // something came in. Now do recv/recvfrom
}

> You usually can't on Unix systems. Use select(2) instead.


> > Please, can anybody give me a part of code, where
> > I can see how to set timeout for opened socket ?

> > Dmitry Avramenko.


 
 
 

How to sets recv socket timeout?

Post by Mikko Tyolajar » Tue, 05 Dec 2000 04:00:00



>Please, can anybody give me a part of code, where
>I can see how to set timeout for opened socket ?

Some systems define a SO_RCVTIMEO socket option, others define it but
do not implement it, yet others define and implement it, but it is
broken.  Many don't have it at all.

For a working, reliable, general solution, use select() or poll().

    $.02,
    /Mikko
--

 RSA Security

 
 
 

How to sets recv socket timeout?

Post by David Schwart » Tue, 05 Dec 2000 04:00:00




> >Please, can anybody give me a part of code, where
> >I can see how to set timeout for opened socket ?

> Some systems define a SO_RCVTIMEO socket option, others define it but
> do not implement it, yet others define and implement it, but it is
> broken.  Many don't have it at all.

> For a working, reliable, general solution, use select() or poll().

        If you're talking about low-performance, single-threaded code that's
only doing one thing at a time, 'alarm' may be perfectly acceptable.

        DS

 
 
 

How to sets recv socket timeout?

Post by Avramenko Dmitr » Wed, 06 Dec 2000 04:00:00


I had in view of following:
When I connect() client socket to server, I want to waiting an answer of
server about 2 secs.

After 2 secs. I close socket with "server response timeout" message.

 
 
 

How to sets recv socket timeout?

Post by Avramenko Dmitr » Wed, 06 Dec 2000 04:00:00


I had in view of following:
When I connect() client socket to server, I want to waiting an answer of
server about 2 secs.

After 2 secs. I close socket with "server response timeout" message.


>Please, can anybody give me a part of code, where
>I can see how to set timeout for opened socket ?

>Dmitry Avramenko.


 
 
 

How to sets recv socket timeout?

Post by David Schwart » Wed, 06 Dec 2000 04:00:00



> I had in view of following:
> When I connect() client socket to server, I want to waiting an answer of
> server about 2 secs.
> After 2 secs. I close socket with "server response timeout" message.

        It is the network stack's job to decide whether a connection can be
made or not. Why would you want to lie to the user and tell them the
server timed out when it didn't? When the connection does in fact
timeout, 'connect' will return an error to you. What you are suggesting
will cause your program to report a timeout when the server is working
and reachable.

        DS

 
 
 

How to sets recv socket timeout?

Post by Nikolai Hristo » Wed, 06 Dec 2000 04:00:00


The way to do it, at least the simplest way I can think is to set an
alarm signal, that will fire in 2 secs.

The signal will be caught by a function that will interrupt the recv
function.

The following is a very basic UDP program that uses timeout with 2 secs
(if I remember correctly)... check the source, it might be of some help.

http://www.linuxnexus.org/SimpleTalk.tar.gz

You will be interested in the SimpleTalkClient::verifiedSend(...)
function

Good Luck,

Nick
------------------
Nikolai Hristov
Denison University
Granville OH 43023


740-587-9272

 
 
 

How to sets recv socket timeout?

Post by Avramenko Dmitr » Thu, 07 Dec 2000 14:23:06


I have 3 hosts on SLIP connections.
server program works on one of them.
2 clients on others.

Clients process certain file in mode of realtime.
Depending on Changes To file  clients asks information from server.

But sometimes SLIP connections losts.
In that case I do not want to wait system connection timeout if connection
is not established in 2 second.



>> I had in view of following:
>> When I connect() client socket to server, I want to waiting an answer of
>> server about 2 secs.

>> After 2 secs. I close socket with "server response timeout" message.

> It is the network stack's job to decide whether a connection can be
>made or not. Why would you want to lie to the user and tell them the
>server timed out when it didn't? When the connection does in fact
>timeout, 'connect' will return an error to you. What you are suggesting
>will cause your program to report a timeout when the server is working
>and reachable.

> DS

 
 
 

How to sets recv socket timeout?

Post by Andrew Giert » Thu, 07 Dec 2000 04:00:00


 Avramenko> I have 3 hosts on SLIP connections.  server program works
 Avramenko> on one of them.  2 clients on others.

 Avramenko> Clients process certain file in mode of realtime.
 Avramenko> Depending on Changes To file clients asks information from
 Avramenko> server.

 Avramenko> But sometimes SLIP connections losts.  In that case I do
 Avramenko> not want to wait system connection timeout if connection
 Avramenko> is not established in 2 second.

If (as appears from your description) there are two slip links between
the client and server then establishing a connection could quite
legitimately take 2 seconds.

But in any case the answer to the general question of how to impose a
controllable timeout on connect() is: either use alarm() to interrupt
the connect call, or use nonblocking mode and select/poll.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

How to sets recv socket timeout?

Post by David Schwart » Thu, 07 Dec 2000 04:00:00



> I have 3 hosts on SLIP connections.
> server program works on one of them.
> 2 clients on others.

> Clients process certain file in mode of realtime.
> Depending on Changes To file  clients asks information from server.

> But sometimes SLIP connections losts.
> In that case I do not want to wait system connection timeout if connection
> is not established in 2 second.

        What if the SLIP connection is heavily loaded and the first packet
attempting to create the connection just happens to get dropped?

        And what do you mean by "wait"? If you have something else to do, you
shouldn't be waiting two seconds. And if you have nothing else to do,
why does it matter how long you wait?

        DS

 
 
 

1. Timeout with sockets [recv()]

Hi...

I'd like to know how to manage timeouts when networking with sockets.
The man-page of setsockopt() speaks about SO_RECVTIMEO, but this #define
doesn't exists (error when compiling)... So, I'd like to know how to
manage timeouts when receiving data with recv().

Thanx.

-- HoTCoDe.
   Council member of The Philament Empire (PhE!), french H/P group.

2. Universal format

3. ? Timeout on socket recv() ?

4. SMC Elite16T Ultra setup

5. some question about the send/recv timeout option usage of TCP socket

6. more memory problems

7. Q: timeout for socket recv?

8. access to Oracle server from FreeBSD

9. How to timeout recv using unix BSD sockets?

10. how to set timeouts and get socket info into my app

11. How to set socket timeout?

12. Setting Unix Socket Timeouts... and good docs on network programming?

13. Setting socket read timeout on Linux