gpt4 book ai didi

java - Android 服务器 PC 客户端

转载 作者:行者123 更新时间:2023-11-30 04:30:02 24 4
gpt4 key购买 nike

我正在尝试让 PC 客户端连接到 Android 服务器。这是在模拟器中运行的。我无法让 PC 客户端连接。

安卓服务器..

try {
serverSocket = new ServerSocket( 1234 );

//tell logcat the server is online
Log.d("TCP", "C: Server Online...");

while ( true ) {
Log.d("TCP", "C: Waiting for client" );

Socket client = serverSocket.accept();

Log.d("TCP", "C: Client has connected" );

BufferedReader in = new BufferedReader(
new InputStreamReader(
client.getInputStream() ) );
String input = in.readLine();

Log.d("TCP", "C: " + input );

client.close();
}
} catch ( Exception e ) {
Log.d( "TCP", "C: " + e );
}

和PC客户端

String host = "127.0.0.1";

//Port the server is running on.
int port = 1234;

//Message to be sent to the PC
String message = "Hello from pc.";
//Get the address of the host.
InetAddress remoteAddr = InetAddress.getByName( host );

//Tell the user that we are attempting a connect.
System.out.println("Attempting connect");

//Make the connection
Socket socket = new Socket(host, port);

//Tell user the connection was established
System.out.println("Connection made");

//Make the output stream to send the data.
OutputStream output = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(output)), true);

//Send the data.
out.println(message);

//Tell user the message was sent
System.out.println("Message sent");

我一直收到 PC 上连接被拒绝的消息。有人可以帮我解决这个问题吗?干杯

最佳答案

来自 Emulator Networking 的文档...

The virtual router for each instance manages the 10.0.2/24 network address space

最重要的是,模拟设备自身的网络地址是 10.0.2.15。

我不使用模拟器,但据我所知,您需要设置从 127.0.0.1:<host-port> 的重定向至 10.0.2.15:<guest-port> .请参阅 Setting up Redirections through the Emulator Console 的文档.

正如我所说,我不使用模拟器,但这是我理解的工作方式。

关于java - Android 服务器 PC 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7882772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com