Please, can anybody give me a part of code, where
I can see how to set timeout for opened socket ?
Dmitry Avramenko.
Dmitry Avramenko.
> Please, can anybody give me a part of code, where
> I can see how to set timeout for opened socket ?
> Dmitry Avramenko.
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
// error, probably interrupt. test for EINTR in errnoQuote:} else if {n < 0) }
// something came in. Now do recv/recvfromQuote:} else if (FD_ISSET(s, &readfds) {
> 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.
For a working, reliable, general solution, use select() or poll().
$.02,
/Mikko
--
RSA Security
> >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().
DS
After 2 secs. I close socket with "server response timeout" message.
After 2 secs. I close socket with "server response timeout" message.
>Dmitry Avramenko.
DS
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
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
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/>
> 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.
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.
3. ? Timeout on socket recv() ?
5. some question about the send/recv timeout option usage of TCP socket
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