Android的服务器PC客户端 [英] Android Server Pc Client
问题描述
我试图让PC客户端连接到Android服务器。这是在模拟器中运行。我不能让PC客户端进行连接。
I'm trying to get a PC client to connect to an Android server. This is running in the emulator. I cannot get the PC client to connect.
Android的服务器。
The android server..
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上拒绝..谁能帮助我?干杯
I keep getting the message connection refused on the PC.. Can anyone help me with this? Cheers
推荐答案
从文档的的模拟器联网 ...
每个实例的虚拟路由器管理10.0.2 / 24网络地址空间
The virtual router for each instance manages the 10.0.2/24 network address space
最重要的是,该仿真设备自己的网络地址是10.0.2.15
Most importantly, the emulated device's own network address is 10.0.2.15.
我不使用模拟器,但据我可以告诉你从 127.0.0.1:<host-port>需要设置一个重定向;
到 10.0.2.15:<guest-port>
。请参阅通过模拟器控制台设置重定向。
I don't use the emulator but as far as I can tell you need to setup a redirection from 127.0.0.1:<host-port>
to 10.0.2.15:<guest-port>
. See the docs for Setting up Redirections through the Emulator Console.
正如我所说,我不使用模拟器,但是这就是我所理解的事情工作。
As I said, I don't use the emulator but this is how I understand things work.
这篇关于Android的服务器PC客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!