Forum Visual Studio

(Non Blocking) Socket BeginReceive Continues receiving not throwing Error when disconnect network

viralchauhanviralchauhan USMember
edited December 2016 in Visual Studio


I trying to make client Socket communication in (Xamarin.Shared) project common for iOS and Android.

I have use System.Net.Sockets.Socket Class with all non - blocking (Async) methods (like Begin*).

I trying to Receive continues data using BeginReceive ** and **receiveCallback


IAsyncResult asyncReceive = clientSocket.BeginReceive( stateObject.sBuffer, 0, stateObject.sBuffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), stateObject);


public static void receiveCallback(IAsyncResult asyncReceive)
StateObject stateObject =

  int bytesReceived = stateObject.sSocket.EndReceive(asyncReceive);

 if (bytesReceived > 0)
    // There  might be more data .Here, you need to Receive again
    clientSocket.BeginReceive(stateObject.buffer, 0, RemainingData.BufferSize, SocketFlags.None ,new AsyncCallback(receiveCallback), stateObject);


  • In this all working fine and data download continuously, but when any kind of network failure (like Wifi OFF) then this cycle is stop working.
  • Also no socketException throws by Socket.

  • How i can get intimation of network fail in continues receiving mode ?

  • Pls suggestion in Xamarin Framework.

In Adavance

Sign In or Register to comment.