Bug 53819 - In Cross form , Net.Socket can't work in iOS but can work in Android.
Summary: In Cross form , Net.Socket can't work in iOS but can work in Android.
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 10.2 (iOS 10.1)
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-03-23 01:59 UTC by hydefate
Modified: 2017-03-29 14:14 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description hydefate 2017-03-23 01:59:00 UTC
Hi all,
this is my code:
it can work in Android , But it can't work in iOS
I use interface with DependencyService to call ModbusTCP in Portable
I don't know why it can't work in iOS.

Thanks.

`
using System;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Modbusbutton3.iOS;
using Xamarin.Forms;
using System.Threading.Tasks;

[assembly: Xamarin.Forms.Dependency(typeof(nModbus))]
namespace Modbusbutton3.iOS
{
public class nModbus : InterfaceFunction
{
static string hostip = "192.168.50.1";
static int port = 5001;
public string Value = ""; 
public bool TimeOut = false; 
public string except = ""; 
public bool CRCCheck = false;
public bool FormatCheck = false;
public int rcvlen = 0;

    public byte[] buffers = new byte[256];
    public bool CRCCheckFunc() { return CRCCheck; }
    public bool FormatCheckFunc() { return FormatCheck; }
    public int rcvlenFunc() { return rcvlen; }


    public byte[] ModbusTCP(int ModbusID, int func, ushort startaddress, int LengthOrValue)
    {

        byte[] senddata;
        byte[] readdata;
        if (func <= 4) { senddata = ReadModbusCommend(ModbusID, func, (ushort)(startaddress - 1), LengthOrValue); }
        else { senddata = WriteSingleCommend(ModbusID, func, (ushort)(startaddress - 1), LengthOrValue); }
        //return byteToHexStr(senddata)+CRCCheck;

        readdata = SocketConnectCommend(hostip, port, senddata); 
        CRCCheck = CRC.CheckCRC(readdata, readdata.Length);
        FormatCheck = CRC.CheckFormat(readdata, senddata);
        //  Value = byteToHexStr(readdata);
        return readdata;
    }

    private byte[] ReadModbusCommend(int ModbusID, int func, ushort StartAddress, int length)
    {
        byte[] count = new byte[2];

        byte[] Commend = new byte[8];

        count = BitConverter.GetBytes(length);

        Commend[0] = Convert.ToByte(ModbusID);
        Commend[1] = Convert.ToByte(func); //function

        Commend[2] = Convert.ToByte((StartAddress & 0xFF00) >> 8);
        Commend[3] = Convert.ToByte(StartAddress & 0x00FF);

        Commend[4] = count[1]; //length
        Commend[5] = count[0];

        //CRC
        Commend[Commend.Length - 2] = Convert.ToByte(CRC.CRC_16(Commend, 6) & 0x00FF);
        Commend[Commend.Length - 1] = Convert.ToByte((CRC.CRC_16(Commend, 6) & 0xFF00) >> 8);

        return Commend;
    }

    private byte[] WriteSingleCommend(int ModbusID, int func, ushort StartAddress, int writevalue)
    {
        byte[] count = new byte[2];
        byte[] Commend = new byte[8];

        count = BitConverter.GetBytes(writevalue);

        Commend[0] = Convert.ToByte(ModbusID);
        Commend[1] = Convert.ToByte(func); //function

        Commend[2] = Convert.ToByte((StartAddress & 0xFF00) >> 8);
        Commend[3] = Convert.ToByte(StartAddress & 0x00FF);

        Commend[4] = count[1]; //length
        Commend[5] = count[0];

        //CRC
        Commend[Commend.Length - 2] = Convert.ToByte(CRC.CRC_16(Commend, 6) & 0x00FF);
        Commend[Commend.Length - 1] = Convert.ToByte((CRC.CRC_16(Commend, 6) & 0xFF00) >> 8);

        return Commend;
    }

    //TCP
    public byte[] SocketConnectCommend(string host, int port, byte[] sendBytes)
    {
        //int port = Convert.ToInt32("502");
        //string host = "192.168.1.204";

        IPAddress ip = IPAddress.Parse(host);
        IPEndPoint ipe = new IPEndPoint(ip, port);
        Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //byte[] sendBytes = strToHexByte("00 00 00 00 00 06 01 05 00 01 00 01 00");
        //  recvBytes= strToHexByte("01 02 03 04 05 06 07 08 09 0A");
        try
        {
            sk.SendTimeout = 3000;
            sk.ReceiveTimeout = 3000;

            sk.Connect(ipe);
            sk.Send(sendBytes, sendBytes.Length, 0);
            rcvlen = sk.Receive(buffers, buffers.Length, 0);
            byte[] rcvdata = new byte[rcvlen];
            for (int i = 0; i < rcvlen; i++)
            {
                rcvdata[i] = buffers[i];
            }
            sk.Close();
            except = "";
            return rcvdata;
        }
        catch (Exception ex)
        {
            except = ex.ToString();
            TimeOut = true;
            return null;
        }
    }

}
}


I get debug log:

System.Net.Sockets.SocketException (0x80004005): Connection timed out
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 
at Modbusbutton3.iOS.nModbus.SocketConnectCommend (System.String host, System.Int32 port, System.Byte[] sendBytes) [0x00034] in D:\APP\Modbusbutton3h20170126\Modbusbutton3\Modbusbutton3\Modbusbutton3.iOS\nModbus.cs:121
Comment 1 Manuel de la Peña [MSFT] 2017-03-23 16:47:55 UTC
Hello,

Can you please provide a complete solution to tests against and the environment you are using to develop, specially the iOS one.
Comment 2 hydefate 2017-03-24 02:00:40 UTC
Sorry , Close The issue , I find it Run on MAC , not Window PC
Comment 3 hydefate 2017-03-29 14:14:10 UTC
Sorry , Close The issue , I find it Run on MAC , not Window PC