Re: How can I tell when a remote TCP connection is closed?



Actually, recv won't fail - it will return zero bytes read for graceful
shutdown.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Phil Frisbie, Jr." <phil@xxxxxxxxxxxx> wrote in message
news:ee7CdRfnFHA.3448@xxxxxxxxxxxxxxxxxxxxxxx
> Jim H wrote:
>
>> I have a client with a TCP connection to a server. Without using events
>> (I need the code to be portable), is there a way that I can tell if the
>> remote end has closed the connection without performing a read or a write
>> on the socket? I tried doing a select for an error condition but it
>> times out. Select on readable or writeable always return as if the remote
>> end is still connected.
>
> select() on readable WILL return true if the server has closed the TCP
> connection gracefully, so perhaps you have something else wrong with your
> code?
>
>> The remote end is doing a shutdown and close on the socket. Netstat
>> shows close_wait. I need to know if the connection has been dropped
>> without having a read or write fail.
>
> After select() returns true for readable, then the recv() will fail with
> the error. That is the ONLY way to do it.
>
>> Can anyone help me out with this?
> >
>> Thanks in advance,
>> jim
>
> --
> Phil Frisbie, Jr.
> Hawk Software
> http://www.hawksoft.com


.



Relevant Pages

  • Re: BackgroundWorker in .NET CF 2.0
    ... ActiveSync 4.5 allows a WiFi connection while cradled. ... private void ManForm_Closing ... I'm making use of this handy utility so I can log debug messages to ... I even added a static ShutDown() method to the TcpTraceListener ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Overlapped WSASend and IOCP
    ... If you want a graceful shutdown then you should call shutdown on the ... then wait for the other end of the connection to do the same. ... What is the best way to disconnect a client from the server. ... grab unsent data from the temporary buffer and retry to send it. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Frequent panics on Dell Latitude D610 running 6.1-STABLE GENERIC
    ... X connection to:0.0 broken (explicit kill or server shutdown). ...
    (freebsd-stable)
  • RE: Does Socket.Shutdown or Socket.Close Block and when?
    ... receive a heartbeat message every 5 seconds on each socket (to make sure the ... connection is still up to each client). ... heartbeat within say 10 seconds then the server will proceed to shutdown the ... the send buffer is empty or becomes empty before the elapsed timeout. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Network connection not getting enabled during shutdown
    ... before during shutdown. ... Your application or service prob will need to monitor the Windows ... monitor that up to the point windows terminates the network connections. ... I'd say it would be impossible to activate a connection once window has ...
    (microsoft.public.win32.programmer.networks)