我可以在本地主机上的简单TCP客户端和AVD Android应用程序上的简单TCP服务器之间创建套接字连接。
AVD在虚拟路由器后面运行,并且要连接到AVD,我需要在虚拟路由器上配置端口转发,就像这样
adb -s emulator-5554转发tcp:5000 tcp:6000
这是一个简单的TCP客户端程序,它在本地主机127.0.0.1
上运行,并连接到在端口6000
上侦听的AVD服务器
public class SimpleTcpClient
{
public static void main(String argv[]) throws Exception
{
String host = "127.0.0.1";
//String host = "10.9.2.121";
int port = 5000;
String sentenceToServer = "hello server";
Socket clientSocket = new Socket(host, port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(sentenceToServer+"\n" );
String serverResponse = inFromServer.readLine();
clientSocket.close();
}
}
这是在android仿真器中运行on app的服务器上监听端口6000
的服务器的初始化方法
private void initTcpTestServer()
{
Log.d("TAG", "initTcpTestServer()");
try
{
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(6000);
welcomeSocket.setReuseAddress(true);
while ( true )
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
clientSentence = "hello client";
outToClient.writeBytes(clientSentence + "\n");
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
如果我在本地主机SimpleTcpClient
上运行127.0.0.1
,则可以创建套接字连接。但是,一旦我尝试在另一台计算机上运行SimpleTcpClient
,我就会得到
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at com.slacorp.eptt.android.testclient.SimpleTcpClient.main(SimpleTcpClient.java:22)
我唯一要做的更改是SimpleTcpClient,是将目标IP地址从本地主机127.0.0.1
更改为10.9.2.121
public class SimpleTcpClient
{
public static void main(String argv[]) throws Exception
{
//String host = "127.0.0.1";
String host = "10.9.2.121";
int port = 5000;
String sentenceToServer = "hello server";
System.out.println("initializing socket");
Socket clientSocket = new Socket(host, port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(sentenceToServer+"\n" );
String serverResponse = inFromServer.readLine();
clientSocket.close();
}
}
SimpleTcpClient
将不会连接到AVD上的服务器,但是如果我使服务器运行在10.9.2.121
上,使得服务器不在虚拟路由器后面的AVD上,则套接字连接有效,所以我感到困惑。
如何将远程TCP客户端连接到AVD?