我使用TCPsocket来通信客户端和服务器。
客户代码:
InetAddress inet = InetAddress.getByName("localhost");
int TCP_SERVER_PORT = 21111;
Socket s = new Socket(inet, TCP_SERVER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String outMsg = "connect" + TCP_SERVER_PORT+System.getProperty("line.separator");
out.write(outMsg);
out.flush();
Log.i("TcpClient", "Client sent - : " + outMsg);
String inMsg = in.readLine() + System.getProperty("line.separator");
textReceived.append("Client received - : " + inMsg);
Log.i("TcpClient", "Client received - : " + inMsg);
s.close();
服务器代码:
ServerSocket ss = null;
int TCP_SERVER_PORT = 21111;
ss = new ServerSocket(TCP_SERVER_PORT);
Socket s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String incomingMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpServer", "received: " + incomingMsg);
textDisplay.append("Server received - : " + incomingMsg);
String outgoingMsg = "Port " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outgoingMsg);
out.flush();
Log.i("TcpServer", "sent: " + outgoingMsg);
textDisplay.append("Server sent - : " + outgoingMsg);
s.close();
我使用单个模拟器在我的系统中测试这个程序。它的工作正常。 现在我需要与两台计算机进行通信。
答案 0 :(得分:0)
由于每个仿真器实例都位于虚拟防火墙后面,因此您需要允许主机上的端口重定向到仿真器实例。
This页面详细介绍了如何通过telnet连接到仿真器实例并启用端口重定向。
如果在每台计算机上运行模拟器,然后设置端口重定向,则应该能够通过指定主机的IP和端口从另一台模拟器实例到达一个模拟器实例。当您有重定向时,您的主机应该将其取出并将其重定向到模拟器实例。