我有一个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());
        }
    }

}

最佳答案

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


任何客户端应用程序(包括浏览器)都可以连接到知名的服务器端点(特定的ip地址和端口),并且仅建立连接即可建立tcp会话,并且从不监听。没问题路由器允许允许传出连接尝试。
任何要连接到正在监听的客户端应用程序的客户端应用程序。这是更困难的,因为处于本地局域网设置的路由器通常不允许任何到本地主机的传入连接尝试。此外,路由器可能正在执行NAT转换,因此它具有一个面向Internet的可寻址IP地址,并且已配置了特定的外部端口以路由到特定的内部主机,该主机可能正在侦听本地地址和端口。


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

07-24 09:38
查看更多