繁体   English   中英

Android Server PC客户端

[英]Android Server Pc Client

我正在尝试让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上收到拒绝消息连接的消息。有人可以帮助我吗? 干杯

从用于仿真器网络的文档...

每个实例的虚拟路由器管理10.0.2 / 24网络地址空间

最重要的是,仿真设备自己的网络地址是10.0.2.15。

我不使用模拟器,但据我所知,您需要设置从127.0.0.1:<host-port>10.0.2.15:<guest-port>的重定向。 请参阅有关通过仿真器控制台设置重定向的文档。

如我所说,我不使用模拟器,但这就是我理解事情的方式。

暂无
暂无

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

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