我有一个Android应用程序(真的是Xamarin应用程序),我使用Socket在 Android 应用程序上侦听端口8888。 所以,我想通过tcp(从PC到Android)从另一台计算机连接到这个端口。
并且,我收到以下错误:
No connection could be made because the target machine actively refused it.
error 10061
我的代码(我从MS示例中获得)适用于两个控制台应用程序。但如果Android是服务器,它就不起作用。
我尝试从电脑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());
}
}
}
答案 0 :(得分:1)
基于套接字的数据传输有两种不同的方案(在典型的本地局域网设置中,有一个普通的isp路由器):
任何连接到知名服务器端点(特定IP地址和端口)的客户端应用程序(包括浏览器),只是连接以建立tcp会话,从不侦听。这没问题。路由器允许允许传出连接尝试。
任何想要连接到正在收听的客户端应用的客户端应用。这更加困难,因为本地局域网设置中的路由器通常不允许对本地主机的任何传入连接尝试。此外,路由器可能正在进行NAT转换,因此它具有可寻址的面向互联网的IP地址,并配置了一个特定的外部端口,以便路由到可能在本地地址和端口侦听的特定内部主机。
有几种方法可以确定此端点寻址映射配置是什么,但它们都不是非常方便。关键点在于,如果该主机先前已尝试通过tcp连接到该外部主机(无论是服务器还是对等客户端应用程序),路由器将仅打开内部主机以进行来自外部互联网的通信。这适用于浏览器/网站服务器配置,因为客户端始终进行连接,因此路由器打开该特定寻址外部服务器的通信通道,并允许客户端(浏览器)接收传入数据。这是udp数据传输的类似情况。如果内部客户端最近向该特定对等体发送了消息,则路由器将仅打开内部主机到外部传入的udp数据。 Http(Tcp变体)提供客户端/服务器协议,该协议使用此路由器约定进行浏览器/网站连接和通信。但对于想要同时充当客户端和服务器的对等客户端,存在重大障碍。当然本地局域网套接字通信没有问题,因为本地IP地址可用于任何内部局域网对等。