Re: Socket Disconnect

From: Paul G. Tobey [eMVP] (ptobey)
Date: 08/31/04


Date: Tue, 31 Aug 2004 08:25:31 -0700

To notice that a connection has been broken, you either have to try to send
something over the socket or you have to tell the network stack to do it for
you. That is, if you are periodically sending packets, you'll find out
roughly two minutes after you send the first packet after the other device
dies that the connection is broken. In that case, the stack will detect
that there has been no acknowledgement. It will retry a number of times,
but, when all of those attempts fail, it will close the socket and you'll
get an error/exception.

If you are sitting around waiting to receive packets, though, you'll never
realize that the other end of the socket is gone, unless you tell the stack
to use keep-alive packets. When you turn that on, the network stack, when
it detects a long period of inactivity, will periodically send a packet to
the other end of the connection. If it replies, the stack goes back to
sleep. If it fails to reply, it will retry, in a certain pattern and, if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
news:ui5tYo2jEHA.1404@TK2MSFTNGP09.phx.gbl...
> Hello All, I am using the below code to create a socket connection via
> wi-fi to another device. The problem is that if the device I am
> connecting
> to losing power and reboots I lose the socket connection however, my
> device
> dosn't know it was dropped and dosn't receive any type of error when it
> sends messages. From what I have read, CF dosn't support SetSocketOption
> so my question is does anyone know of a good way to work around this?
>
>
> ****SocketConnect****
> g_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream
> ,ProtocolType.Tcp );
> IPAddress ip = IPAddress.Parse (strRemoteIPBase);
> IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
> g_socClient.Connect ( ipEnd );
>
> WaitForData();
>
>
>
> ****WaitForData ****
>
> if ( pfnCallBack == null )
> {
> pfnCallBack = new AsyncCallback (OnDataReceived);
> }
> CSocketPacket theSocPkt = new CSocketPacket ();
> theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;
>
> m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
> ,0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnCallBack,theSocPkt);
>
>



Relevant Pages

  • Re: Selecting optimum block sizes for data transmission
    ... socket up with data until it either tells you you can't send any more ... Once you've make a call to send data, the TCP/IP stack does a huge ... Maintains a congestion window to limit data flow in the face of long ... Attempts to avoid wasteful sends of very small packets, ...
    (comp.lang.pascal.delphi.misc)
  • Large stack causes WSAENOBUFS error
    ... I started to get WSAENOBUFS errors from my code that uses the libpq ... It seems that when a nonblocking socket connection is requested when the ... smaller stack sizes and for blocking sockets. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket Disconnect
    ... That is, if you are periodically sending packets, you'll find out ... > dies that the connection is broken. ... In that case, the stack will detect ... > but, when all of those attempts fail, it will close the socket and you'll ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Large stack causes WSAENOBUFS error
    ... socket routine, but had missed increasing the stack for parallel threads. ... int main{ ... It seems that when a nonblocking socket connection is requested when the ...
    (microsoft.public.win32.programmer.networks)
  • Re: CAsyncSocket thread crashing on WM_SOCKET_NOTIFY message
    ... > getting a message after the socket has closed. ... > packets for the connection/socket that ends in an exception. ... The server is listening to port 33000. ... > Close Connection ...
    (microsoft.public.vc.mfc)

Loading