You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by C K Tan <ck...@nxscientific.com> on 2002/08/20 19:48:08 UTC
apr_socket connection status check
Hi, I am looking for a way to check if a socket is still connected to
the client.
What is a good way to do this in APR? Here are some tries which do not
work
(on Win32 with the cvs co latest):
1) send 0 bytes -- failed to detect connection loss, probably due to
buffering:
int n = 0;
int e = apr_send(sock, (char*) NULL, &n);
if (e) not_connected();
2) recv 0 bytes -- blocked in definitely:
int n = 0;
int e = apr_recv(sock, (char*) NULL, &n);
if (e) not_connected();
3) query APR_SO_DISCONNECTED -- always return connection OK:
int n, e;
e = apr_socket_opt_get(sock, APR_SO_DISCONNECTED, &n);
if (e || n) not_connected();
Thanks,
-cktan
RE: apr_socket connection status check
Posted by Bill Stoddard <bi...@wstoddard.com>.
>
> On Wed, Aug 21, 2002 at 01:48:08AM +0800, C K Tan wrote:
> > Hi, I am looking for a way to check if a socket is still connected to
> > the client.
> > What is a good way to do this in APR? Here are some tries which do not
> > work
> > (on Win32 with the cvs co latest):
>
> Use apr_poll and look for the out of band errors.
>
> flood uses the following to check the socket:
>
> apr_status_t check_socket(flood_socket_t *s, apr_pool_t *pool)
> {
> apr_status_t e;
> apr_int32_t socketsRead;
> apr_pollfd_t pout;
> apr_int16_t event;
>
> pout.desc_type = APR_POLL_SOCKET;
> pout.desc.s = s->socket;
> pout.reqevents = APR_POLLIN | APR_POLLPRI | APR_POLLERR |
> APR_POLLHUP | APR_POLLNVAL;
> pout.p = pool;
>
> e = apr_poll(&pout, 1, &socketsRead, 1000);
> if (socketsRead && pout.rtnevents) {
> return APR_EGENERAL;
> }
>
> return APR_SUCCESS;
> }
>
> Note that flood checks for POLLIN because it is a client and shouldn't
> see anything incoming that it hasn't requested, so that would be an
> error.
>
> HTH. -- justin
So if poll returns -1, this function will return APR_SUCCESS? That doesn't
seem right.
Bill
RE: apr_socket connection status check
Posted by Bill Stoddard <bi...@wstoddard.com>.
>
> On Wed, Aug 21, 2002 at 01:48:08AM +0800, C K Tan wrote:
> > Hi, I am looking for a way to check if a socket is still connected to
> > the client.
> > What is a good way to do this in APR? Here are some tries which do not
> > work
> > (on Win32 with the cvs co latest):
>
> Use apr_poll and look for the out of band errors.
>
> flood uses the following to check the socket:
>
> apr_status_t check_socket(flood_socket_t *s, apr_pool_t *pool)
> {
> apr_status_t e;
> apr_int32_t socketsRead;
> apr_pollfd_t pout;
> apr_int16_t event;
>
> pout.desc_type = APR_POLL_SOCKET;
> pout.desc.s = s->socket;
> pout.reqevents = APR_POLLIN | APR_POLLPRI | APR_POLLERR |
> APR_POLLHUP | APR_POLLNVAL;
> pout.p = pool;
>
> e = apr_poll(&pout, 1, &socketsRead, 1000);
> if (socketsRead && pout.rtnevents) {
> return APR_EGENERAL;
> }
>
> return APR_SUCCESS;
> }
>
> Note that flood checks for POLLIN because it is a client and shouldn't
> see anything incoming that it hasn't requested, so that would be an
> error.
>
> HTH. -- justin
>
So if poll returns -1, this function will return APR_SUCCESS? That doesn't
seem right.
Bill
Re: apr_socket connection status check
Posted by Justin Erenkrantz <je...@apache.org>.
On Wed, Aug 21, 2002 at 01:48:08AM +0800, C K Tan wrote:
> Hi, I am looking for a way to check if a socket is still connected to
> the client.
> What is a good way to do this in APR? Here are some tries which do not
> work
> (on Win32 with the cvs co latest):
Use apr_poll and look for the out of band errors.
flood uses the following to check the socket:
apr_status_t check_socket(flood_socket_t *s, apr_pool_t *pool)
{
apr_status_t e;
apr_int32_t socketsRead;
apr_pollfd_t pout;
apr_int16_t event;
pout.desc_type = APR_POLL_SOCKET;
pout.desc.s = s->socket;
pout.reqevents = APR_POLLIN | APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL;
pout.p = pool;
e = apr_poll(&pout, 1, &socketsRead, 1000);
if (socketsRead && pout.rtnevents) {
return APR_EGENERAL;
}
return APR_SUCCESS;
}
Note that flood checks for POLLIN because it is a client and shouldn't
see anything incoming that it hasn't requested, so that would be an
error.
HTH. -- justin