You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by David Barrett <db...@quinthar.com> on 2004/09/30 00:14:14 UTC

APR Socket Questions

Ok, now I'm starting to move over my socket code, and I've come up with the
following questions:

1) If I want to set a socket to be non-blocking, is it enough to set:
"apr_socket_opt_set( s, APR_SO_NONBLOCK, 1 )", or need I also set
"apr_socket_timeout_set( s, 0 )"?  This link
(http://marc.theaimsgroup.com/?l=apr-dev&m=106562047130592&w=2) seems to
indicate I need to do both, but it's almost a year old -- what's the latest
scoop? 

2) Is "apr_poll( )" the equivalent to "select( )"?  How do I convert an
"apr_socket_t" into a "apr_pollfd_t" in order to add to the pollset?

3) When I open a non-blocking socket to a remote client, how do I know if
the connection succeeds?  Normally I would "select( )" on the socket until
it's writeable (success) or an error (failure).  What's the APR equivalent? 

4) If I call "apr_socket_recv( )" on a non-blocking socket, how do I
distinguish between data being available, a graceful shutdown, or an actual
error? In essence, what's the portable equivalent to:

# int numRead = recv( s, ... );
# if( numRead == SOCKET_ERROR ) {
#    int error = WSAGetLastError( );
#    if( error == WSAEWOULDBLOCK ) {
#       // No data available, but no error otherwise
#    } else {
#       // Actual read error
#    }
# } else if( numRead == 0 ) {
#    // Graceful shutdown initiated by remote side
# } else {
#    // Data available
#    assert( numRead > 0 );
# }

5) How do I bind a socket to listen on an unspecified port?  Normally I'd
set "sockaddr_in.sin_port = INADDR_ANY" when using "bind( )".  However, APR
defines "APR_ANYADDR" to be the string "0.0.0.0".  Should I use "APR_UNSPEC"
instead?

6) When I bind a socket to a local port for listening, how can I determine
which port it was bound to?  The sample uses "apr_sockaddr_port_get( )", but
this function doesn't seem to exist in my APR installation (Unix nor Wi32).
("apr_sockaddr_ip_get( )" works fine, though.)  Is my install (apr-1.0.0)
broken?  As a workaround, will using "ntohs( sockAddr->sa.sin.sin_port )" be
portable?

7) When setting a socket to listen on a port, how can I choose the maximum
backlog?  Normally I'd say "listen( s, SOMAXCONN )".  Should I just use
"apr_socket_listen( s, SOMAXCONN )", or is there an APR replacement for
SOMAXCONN?


This is my first attempt at real cross-platform development and compilation,
and APR is proving to be a tremendous help.  It's great to have so much
functionality under one roof.  Thanks again!

-david