【问题标题】:Can't connect to TCP port on Android无法连接到 Android 上的 TCP 端口
【发布时间】:2016-06-20 23:06:39
【问题描述】:

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

而且,我收到以下错误:

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

我的代码(我从 MS 示例中获得)适用于两个控制台应用程序。但是如果安卓是服务器就不行了。

我尝试从电脑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());
        }
    }

}

【问题讨论】:

  • 几乎可以肯定是网络配置错误。如果您的 android 设备在蜂窝网络上,它将永远无法工作 - 蜂窝网络不允许请求通过。如果您的设备在 wifi 上,您需要确保您的路由器允许请求通过。
  • 是的,我使用 wifi。谢谢,我查一下!
  • IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); 在 127.0.0.1 上收听是没有意义的。

标签: c# android sockets xamarin xamarin.android


【解决方案1】:

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

  1. 任何连接到已知服务器端点(特定 IP 地址和端口)并仅连接以建立 tcp 会话且从不侦听的客户端应用程序(包括浏览器)。这没有问题。路由器允许允许传出连接尝试。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2018-07-28
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2012-11-13
    相关资源
    最近更新 更多