什么是ExtendedSocket异常,我该如何解决?

时间:2018-11-24 04:36:50

标签: c# .net sockets xamarin client

每当尝试使用System.Net.Sockets和Xamarin将数据发送到c#中的服务器时,都会出现错误。我仅在发送数据但未连接时收到此错误。错误消息是     “抛出异常:     中的'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException'     System.Net.Sockets.dll” 我不确定如何解决此问题,但是当按下按钮时,这是我的连接代码正在运行。

        Label label = this.FindByName<Label>("label");
        Socket sock;

        string ipI = "127.0.0.1";
        int portI = 6419;

        IPAddress ipAddress = IPAddress.Parse(ipI);

        IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, portI);

        sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        sock.Connect(remoteEndPoint);
        byte[] sData = Encoding.ASCII.GetBytes(num);
        sock.Send(sData);
        label.Text = "Added 1 to server";
        sock.Dispose();

1 个答案:

答案 0 :(得分:-1)

尝试使用tcpclient代替websocket:

        try
        {
            TcpClient client = new TcpClient(127.0.0.1, 6419);

            NetworkStream ns = client.GetStream();

            byte[] bytes = new byte[1024];
            int bytesRead = await ns.ReadAsync(bytes, 0, bytes.Length);
            string result = Encoding.ASCII.GetString(bytes, 0, bytesRead);

            client.Close();
            return result;
        }

        catch (Exception e)
        {
            return null;
        }