Android Server Pc客户端

时间:2011-10-24 22:36:21

标签: java android

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

android服务器..

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上拒绝邮件连接..任何人都可以帮我这个吗?干杯

1 个答案:

答案 0 :(得分:1)

来自Emulator Networking的文档......

  

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

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

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

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