How do I let connect(2) timeout? - Unix

This is a discussion on How do I let connect(2) timeout? - Unix ; Hi, We are using connect(2) to access Unix Domain Socket, and if for some reason the connect(2) will fail, we don't want the caller wait for so long. So we want to implement something like: int my_connect(int server_sock, const struct ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: How do I let connect(2) timeout?

  1. How do I let connect(2) timeout?

    Hi,

    We are using connect(2) to access Unix Domain Socket, and if for some
    reason the connect(2) will fail, we don't want the caller wait for so
    long. So we want to implement something like:

    int my_connect(int server_sock, const struct sockaddr *serv_addr,
    socklen_t addrlen, unsigned int timeout );

    By this way, the caller can pass a timeout value ( serval seconds ) to
    my_connect().

    What is the simple way to implement it? Should I `fcntl' the
    server_sock to turn on its NONBLOCK flag? Then what can we do? The
    man page here is not so clear to me, it seems after
    fcntl( server_sock ), I can use select:


    EINPROGRESS
    The socket is non-blocking and the connection cannot be
    completed
    immediately. It is possible to select(2) or poll(2)
    for comple-
    tion by selecting the socket for writing. After
    select(2) indi-
    cates writability, use getsockopt(2) to read the
    SO_ERROR option
    at level SOL_SOCKET to determine whether connect()
    completed suc-
    cessfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR
    is one of
    the usual error codes listed here, explaining the
    reason for the
    failure).

    Does it mean I should use select on the server_sock to test if it
    ready for writing? I think the right descriptor I can write is only
    the descriptor that connect(2) returned for me after it successfully
    returned.

    BTW: The OS is Linux

    Thanks in advance.

    -
    narke

  2. Re: How do I let connect(2) timeout?

    On May 26, 5:48 pm, Steven Woody wrote:
    > Hi,
    >
    > We are using connect(2) to access Unix Domain Socket, and if for some
    > reason the connect(2) will fail, we don't want the caller wait for so
    > long. So we want to implement something like:
    >
    > int my_connect(int server_sock, const struct sockaddr *serv_addr,
    > socklen_t addrlen, unsigned int timeout );
    >
    > By this way, the caller can pass a timeout value ( serval seconds ) to
    > my_connect().
    >
    > What is the simple way to implement it? Should I `fcntl' the
    > server_sock to turn on its NONBLOCK flag? Then what can we do? The
    > man page here is not so clear to me, it seems after
    > fcntl( server_sock ), I can use select:
    >
    > EINPROGRESS
    > The socket is non-blocking and the connection cannot be
    > completed
    > immediately. It is possible to select(2) or poll(2)
    > for comple-
    > tion by selecting the socket for writing. After
    > select(2) indi-
    > cates writability, use getsockopt(2) to read the
    > SO_ERROR option
    > at level SOL_SOCKET to determine whether connect()
    > completed suc-
    > cessfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR
    > is one of
    > the usual error codes listed here, explaining the
    > reason for the
    > failure).
    >
    > Does it mean I should use select on the server_sock to test if it
    > ready for writing? I think the right descriptor I can write is only
    > the descriptor that connect(2) returned for me after it successfully
    > returned.
    >
    > BTW: The OS is Linux
    >
    > Thanks in advance.
    >
    > -
    > narke


    please :-)

  3. Re: How do I let connect(2) timeout?

    In article
    <394f3a1d-b24e-4e09-8375-af9a866f6981@b5g2000pri.googlegroups.com>,
    Steven Woody wrote:

    > We are using connect(2) to access Unix Domain Socket, and if for some
    > reason the connect(2) will fail, we don't want the caller wait for so
    > long. So we want to implement something like:
    >
    > int my_connect(int server_sock, const struct sockaddr *serv_addr,
    > socklen_t addrlen, unsigned int timeout );
    >
    > By this way, the caller can pass a timeout value ( serval seconds ) to
    > my_connect().
    >
    > What is the simple way to implement it? Should I `fcntl' the
    > server_sock to turn on its NONBLOCK flag? Then what can we do? The
    > man page here is not so clear to me, it seems after
    > fcntl( server_sock ), I can use select:


    Use select(), and use its timeout parameter to specify how long to wait
    for the connect() to complete.

    > Does it mean I should use select on the server_sock to test if it
    > ready for writing? I think the right descriptor I can write is only
    > the descriptor that connect(2) returned for me after it successfully
    > returned.


    connect() doesn't return a socket, it takes a socket as input and uses
    it; it returns 0 when successful, -1 if not. You're thinking of
    accept(), which takes a listening socket and returns a different,
    connected socket.

    If you set the socket non-blocking before calling connect(), connect()
    will normally return -1 with ERRNO set to EWOULDBLOCK. Then you call
    select(), waiting for writability of the socket, and that indicates that
    it's connected.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  4. Re: How do I let connect(2) timeout?

    On May 27, 2:07 am, Barry Margolin wrote:
    > In article
    > <394f3a1d-b24e-4e09-8375-af9a866f6...@b5g2000pri.googlegroups.com>,
    > Steven Woody wrote:
    >
    > > We are using connect(2) to access Unix Domain Socket, and if for some
    > > reason the connect(2) will fail, we don't want the caller wait for so
    > > long. So we want to implement something like:

    >
    > > int my_connect(int server_sock, const struct sockaddr *serv_addr,
    > > socklen_t addrlen, unsigned int timeout );

    >
    > > By this way, the caller can pass a timeout value ( serval seconds ) to
    > > my_connect().

    >
    > > What is the simple way to implement it? Should I `fcntl' the
    > > server_sock to turn on its NONBLOCK flag? Then what can we do? The
    > > man page here is not so clear to me, it seems after
    > > fcntl( server_sock ), I can use select:

    >
    > Use select(), and use its timeout parameter to specify how long to wait
    > for the connect() to complete.
    >
    > > Does it mean I should use select on the server_sock to test if it
    > > ready for writing? I think the right descriptor I can write is only
    > > the descriptor that connect(2) returned for me after it successfully
    > > returned.

    >
    > connect() doesn't return a socket, it takes a socket as input and uses
    > it; it returns 0 when successful, -1 if not. You're thinking of
    > accept(), which takes a listening socket and returns a different,
    > connected socket.
    >
    > If you set the socket non-blocking before calling connect(), connect()
    > will normally return -1 with ERRNO set to EWOULDBLOCK. Then you call
    > select(), waiting for writability of the socket, and that indicates that
    > it's connected.
    >
    > --
    > Barry Margolin, bar...@alum.mit.edu
    > Arlington, MA
    > *** PLEASE post questions in newsgroups, not directly to me ***
    > *** PLEASE don't copy me on replies, I'll read them in the group ***


    I did mess up accept() and connect(), thank you, I got it.

+ Reply to Thread