简体   繁体   English

Android Server PC客户端

[英]Android Server Pc Client

I'm trying to get a PC client to connect to an Android server. 我正在尝试让PC客户端连接到Android服务器。 This is running in the emulator. 这正在模拟器中运行。 I cannot get the PC client to connect. 我无法连接PC客户端。

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 );
    }

and the PC client 和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");

I keep getting the message connection refused on the PC.. Can anyone help me with this? 我一直在PC上收到拒绝消息连接的消息。有人可以帮助我吗? Cheers 干杯

From the docs for Emulator Networking ... 从用于仿真器网络的文档...

The virtual router for each instance manages the 10.0.2/24 network address space 每个实例的虚拟路由器管理10.0.2 / 24网络地址空间

Most importantly, the emulated device's own network address is 10.0.2.15. 最重要的是,仿真设备自己的网络地址是10.0.2.15。

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> . 我不使用模拟器,但据我所知,您需要设置从127.0.0.1:<host-port>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. 如我所说,我不使用模拟器,但这就是我理解事情的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM