The socket has been shut down (


I am implementing an APP that will connect the mobile phone to a device. And communicate with modbus tcp.

(Device: Samsung s9, Android 10, Target API 29)

When I connect my mobile phone to the device on my own(Not programatically. Connect by my self in wifi setting page.) The modbus TCP works fine.

But when I connect to the device through "WifiNetworkSpecifier". It always returns exception:The socket has been shut down.(Code from here: ref)

I have checked into the modbus tcp function. During the two test. The IP & Port are exactly the same.

Here is my code.

//Connect to target device
var specifier = new WifiNetworkSpecifier.Builder()
var request = new NetworkRequest.Builder()
    .AddTransportType(TransportType.Wifi) // we want WiFi
    .RemoveCapability(NetCapability.Internet) // Internet not required
    .SetNetworkSpecifier(specifier) // we want _our_ network
var callback = new NetworkCallback
    NetworkAvailable = network =>
        // we are connected!
ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
connectivityManager.RequestNetwork(request, callback);


//Send modbus tcp cmd
public byte[] SocketConnectCommend(string host, int port, byte[] sendBytes)
    int retry = 0;
    IPAddress ip = IPAddress.Parse(host);
    IPEndPoint ipe = new IPEndPoint(ip, port);
    Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        sk.SendTimeout = 3000;
        sk.ReceiveTimeout = 3000;
        IAsyncResult result = sk.BeginConnect(ip, port, null, null);

        bool success = result.AsyncWaitHandle.WaitOne(1000, true);

        if (!success)
            throw new ApplicationException("Failed to connect server.");

        // sk.ConnectAsync(ipe);
        sk.Send(sendBytes, sendBytes.Length, 0);    //Exception throw at here

        rcvlen = sk.Receive(buffers, buffers.Length, 0);

        byte[] rcvdata = new byte[rcvlen];
        for (int i = 0; i < rcvlen; i++)
            rcvdata[i] = buffers[i];

        except = "";
        return rcvdata;

    catch (Exception ex)
        except = ex.ToString();
        Isbusy = false;
        TimeOut = true;
        //if (retry >= 5)
        { return null; }

The exception always throw at

sk.Send(sendBytes, sendBytes.Length, 0);

Message of exception:

{System.Net.Sockets.SocketException (0x80004005): The socket has been shut down at System.Net.Sockets.Socket.Send (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags) [0x00010] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/System/net/System/Net/Sockets/Socket.cs:1542 at System.Net.Sockets.Socket.Send (System.Byte[] buffer, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/System/net/System/Net/Sockets/Socket.cs:1301 at Modbusbutton3.Droid.nModbus.SocketConnectCommend (System.String host, System.Int32 port, System.Byte[] sendBytes) [0x00072] in D:\APP\V1.31\Modbusbutton3\Modbusbutton3.Droid\nModbus.cs:186 }

How can I solve this?

asked on Stack Overflow Nov 18, 2020 by CC.Wang • edited Nov 18, 2020 by CC.Wang

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0