[英]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.