[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] What is the best way to detect a client socket disconnect?



On Wed, Nov 24, 2010 at 10:43 AM, Ken Feng <kfmfe04@xxxxxxxxx> wrote:
> Hi Kevin,
>
> I appreciate the quick reply.  After reading Nick's book, that's what
> I suspected, but I can't seem to get it to work.  I essentially took
> Nick's TCP Client code, expanded it a bit to include an echo client,
> and started a server thread and a client thread.  The client sends a
> hello world, which it gets back from the server, and then calls
> event_base_loopexit( base, NULL ); to get out.  However, the server's
> event_cb isn't invoked:
>
> http://pastebin.com/fzwpE1Wm
>
> I compiled using:
> g++ Test_libevent.cpp -o Test_libevent -levent_core -levent_pthreads
> && ./Test_libevent
>
> so I apologize for any C++-isms that got in there.  I hope someone
> more experienced than I can take a quick look and tell me what I'm
> doing wrong - I hope it's something obvious/simple.

FWICT, your code isn't really simulating a cutoff: the file descriptor
allocated for the client bufferevent at line 128 is never actually
closed, since you neither close the socket nor free the bufferevent.
(There's a bufferevent_free call in client_event_cb, but it will never
get invoked, unless the server closes the connection or causes an
error, I think.)  Thus, sockfd won't get automatically freed until the
entire process exits: just exiting the client thread isn't enough to
make anything happen to it.

hth,
-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.