c# - 无法连接到 Android 上的 TCP 端口

标签 c# android sockets xamarin xamarin.android

我有一个 Android 应用程序(真正的 Xamarin 应用程序),我正在使用 Socket 来监听 Android 应用程序上的端口 8888。 所以,我想通过 tcp(从 PC 到 Android)从另一台计算机连接到这个端口。

而且,我收到以下错误:

No connection could be made because the target machine actively refused it.
error 10061

我的代码(我从 MS 示例中获得)适用于两个控制台应用程序。但如果 Android 是服务器,它就不起作用。

我尝试从 pc ping 手机,没问题。

我的问题:也许应该打开一个端口?或者是其他东西?我该怎么做?

我很乐意倾听任何想法。谢谢。

我的服务器代码。 C#、Xamarin。

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Android.Runtime;
using Java.Lang;
using Byte = System.Byte;
using Exception = System.Exception;
using String = System.String;
using StringBuilder = System.Text.StringBuilder;

// State object for reading client data asynchronously
public class StateObject
{
    // Client  socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

public class AsynchronousSocketListener
{
    // Thread signal.
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public AsynchronousSocketListener()
    {
    }

    public static void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8888);
        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.
        try
        {
            listener.Bind(remoteEP);
            listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);



                // Wait until a connection is made before continuing.
                allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer, 0, bytesRead));

            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1)
            {
                // All the data has been read from the 
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                    content.Length, content);
                // Echo the data back to the client.
                Send(handler, content);

            }
            else
            {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }

    private static void Send(Socket handler, String data)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

}

我的客户端代码。只是简单的 C# 控制台应用程序:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketClient
{

    public static void StartClient() {
        // Data buffer for incoming data.
        byte[] bytes = new byte[1024];

        Console.WriteLine("Start!");

        // Connect to a remote device.
        try {
            // Establish the remote endpoint for the socket.
            // This example uses port 11000 on the local computer.


           IPAddress ipAddress = new IPAddress(new byte[] { 10, 0, 1, 173 });
         //   IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8888);

            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork, 
                SocketType.Stream, ProtocolType.Tcp );

            // Connect the socket to the remote endpoint. Catch any errors.
            try {
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected to {0}",
                    sender.RemoteEndPoint.ToString());

                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

                // Send the data through the socket.
                int bytesSent = sender.Send(msg);

                // Receive the response from the remote device.
                int bytesRec = sender.Receive(bytes);
                Console.WriteLine("Echoed test = {0}",
                    Encoding.ASCII.GetString(bytes,0,bytesRec));

                // Release the socket.
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

            } catch (ArgumentNullException ane) {
                Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
            } catch (SocketException se) {

                Console.WriteLine("SocketException : {0}",se.ErrorCode);
                Console.WriteLine("SocketException : {0}",se.SocketErrorCode);
                Console.WriteLine("SocketException : {0}",se.ToString());
            } catch (Exception e) {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

        } catch (Exception e) {
            Console.WriteLine( e.ToString());
        }
    }

}

最佳答案

基于套接字的数据传输有两种不同的场景(在典型的本地 LAN 设置中,那里有一个普通的 isp 路由器):

  1. 任何客户端应用程序(包括浏览器)连接到一个众所周知的服务器端点(特定的 ip 地址和端口)并且只是连接以建立一个 tcp session ,并且从不监听。这没问题。路由器允许传出连接尝试。

  2. 任何想要连接到另一个正在监听的客户端应用程序的客户端应用程序。这更加困难,因为本地 LAN 设置中的路由器通常不允许任何到本地主机的传入连接尝试。此外,路由器可能正在进行 NAT 转换,因此它具有可寻址的面向 Internet 的 IP 地址,并配置了特定的外部端口以路由到可能正在监听本地地址和端口的特定内部主机。

有几种方法可以确定此端点寻址映射配置是什么,但都不是很方便。关键点是,如果该主机先前已尝试通过 tcp 连接到该外部主机(无论是服务器还是对等客户端应用程序),则路由器只会打开内部主机以进行来自外部互联网的通信。这适用于浏览器/网站服务器配置,因为客户端始终在进行连接,因此路由器会为该特定地址的外部服务器打开通信 channel ,并允许客户端(浏览器)接收传入数据。 udp数据传输也是类似的情况。如果内部客户端最近向特定对等方发送了消息,则路由器只会将内部主机开放给外部传入的 udp 数据。 Http(一种 Tcp 变体)提供使用此路由器约定进行浏览器/网站连接和通信的客户端/服务器协议(protocol)。但是对于既要充当客户端又要充当服务器的对等客户端来说,存在很大的障碍。当然,本地 lan 套接字通信没有问题,因为本地 ip 地址可用于任何内部 lan 对等点。

关于c# - 无法连接到 Android 上的 TCP 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843057/

相关文章:

android - 模拟器: socketTcpLoopbackClientFor: error: fd 52396 above FD_SETSIZE (32768) but the Emulator seems to run properly

c# - 用字典绑定(bind)下拉列表

c# - 找不到包 'Microsoft.AspNet.WebApi.Client' .Net Core 2.2 的编译库位置

c# - 应用范围内的 Observable 集合

Java 拆分大写字符串并将其全部转换为小写

c - 段错误问题

c# - .NET 线程 - 这是处理返回结果的两个线程的合适方法吗?

android - 在这种情况下溢出意味着什么?

android - 方向更改后如何将 fragment 重新加载到后台堆栈

java - Android 中的套接字异常