apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bennett, Tony - CNF" <Bennett.T...@cnf.com>
Subject RE: APR Socket Questions
Date Wed, 29 Sep 2004 22:42:13 GMT
In addition to this response,
you should read the code for the APR functions you want/need to use.

See below: 

-----Original Message-----
From: David Barrett [mailto:dbarrett@quinthar.com] 
Sent: Wednesday, September 29, 2004 3:14 PM
To: dev@apr.apache.org
Subject: 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? 

I've not used non-blocking... sorry.


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?

Example of waiting for a connect to complete:
            apr_poll_setup(&readbits, 1, pool);
            apr_poll_socket_add(readbits, sock, APR_POLLOUT);
            nbr_connects = 1;
            status = apr_poll(readbits, 1, &nbr_connects, 5 *
APR_USEC_PER_SEC);
            if (     status       != APR_SUCCESS
                  || nbr_connects == 0 )
            {
                 apr_socket_close(*sock);
           stuff deleted


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? 

sorry...


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?

use apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
where sa's sin_port is set to zero.

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?

Sorry...


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?

Searching APR doesn't reveal SOMAXCONN being used.


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


Mime
View raw message