SelectVS. Alarm for socket timeout

SelectVS. Alarm for socket timeout

Post by Rya » Fri, 09 Nov 2001 01:21:23



I have a process that opens a single socket connection to a remote
server.  I would like to implement a timeout for this connection and
was wondering:

A. if select() and alarm() could both handle all timeout scenarious?

and

B. which is least costly regarding system resources?

Your input is very much appreciated!

Ryan Mahoney

 
 
 

SelectVS. Alarm for socket timeout

Post by Arnold Hendrik » Fri, 09 Nov 2001 03:10:46



> I have a process that opens a single socket connection to a remote
> server.  I would like to implement a timeout for this connection and
> was wondering:
> A. if select() and alarm() could both handle all timeout scenarious?

select is generally more flexible, and can do all alarm() can do.
OTOH, alarm() may be simpler to use when dealing with just one
connection.

Quote:> and
> B. which is least costly regarding system resources?

That depends on your system. I doubt you'll notice the difference
with just one connection.

--

B-Lex Information Technologies, http://www.b-lex.com/

 
 
 

1. Socket timeout using alarm.

I tried to use the nonblocking and select to implement the socket timeout,
but it was too hard for me so I decided to use alarm function for this.

Here is what I did :

            alarm(3);
            if (connect(fd, (struct sockaddr *)(&dest), sizeof(struct
sockaddr_in)) < 0) {
                if (errno == EINTR) {
                    printf("connect error\n");
                    exit(0);
                }
                else {
                    perror("connect");
                    exit(1);
                }
            }
            alarm(0);

This seems to be working fine so far.

When I try to connect to the wrong server ip address,
it waits for three seconds and gives me the 'connect error' message.

When I use the right server ip address, it connects to it and sends the
message to the server.

I have tried to add the 'SIGNAL' thing to this code.

But don't know what that 'SIGNAL' really does and how to add it to my above
source code.

I went through the 'Unix Network Programming by Stevens',
but this book is too hard for someone like me who is not so smart at all. :(

Thanks.

2. Looking for some bootstrap help.

3. Socket connect timeout with alarm()

4. Loadlin 1.5 fails with WIN95

5. socket timeout? (socket programming question)

6. How can I make my screen larger?

7. use alarm() to achieve timeout.

8. More on ROOT password recovery

9. Timeout module for general use, signal/alarm

10. <alarm call> systemmessage with threads and socket

11. Help on Design: Socket, fdopen, alarm()

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

13. How to change retransmission timeout for a TCP socket?