简体   繁体   English

如何建立TCP连接并将数据发送到Android虚拟设备应用

[英]How to make TCP connection and send data to Android Virtual Device App

I am creating and testing a simple TCP server on an Android emulator. 我正在Android模拟器上创建和测试简单的TCP服务器。

I use a simple Java client program to try to connect to the server running on the emulator. 我使用一个简单的Java客户端程序尝试连接到在模拟器上运行的服务器。 I attempt to send a simple string like "hello world". 我尝试发送一个简单的字符串,例如“ hello world”。

I think the connection between the client and server is successfully initialized; 我认为客户端和服务器之间的连接已成功初始化; however, data is not routed to the Android device. 但是,数据不会路由到Android设备。

The server thread blocks at line clientSentence = inFromClient.readLine(); 服务器线程在clientSentence = inFromClient.readLine();clientSentence = inFromClient.readLine(); and the client thread blocks at String serverResponse = inFromServer.readLine(); 并且客户端线程在String serverResponse = inFromServer.readLine(); .

I have port forwarded local host port 6100 to AVD virtual port 7100 as per Google docs with ADB 我已经根据具有ADB Google 文档将本地主机端口6100端口转发到了AVD虚拟端口7100

adb -s emulator-5554 forward tcp:6100 tcp:7100

Here is Java class TCPTestClient 这是Java类TCPTestClient

public class TCPTestClient
{
   public static void main(String argv[]) throws Exception
   {
      String sentenceToServer = "hello server";
      System.out.println("initializing socket");
      Socket clientSocket = new Socket("127.0.0.1", 6100);
      System.out.println("socket initialized");
      System.out.println("getting output stream to server");
      DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
      System.out.println("found output stream to server");
      System.out.println("getting input stream from server");
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      System.out.println("found input stream from server");
      System.out.println("writing sentence to server");
      outToServer.writeBytes(sentenceToServer );
      System.out.println("sentence written");
      System.out.println("waiting for sentence response from server");
      String serverResponse = inFromServer.readLine();
      System.out.println("serverResponse = "+serverResponse);
      System.out.println("socket closed");
      clientSocket.close();
   }
}

Here is Android app method initTcpTestServer() 这是Android应用程序方法initTcpTestServer()

private void initTcpTestServer()
{
  Log.d("TAG", "initTcpTestServer()");
  try
  {

     String clientSentence;
     ServerSocket welcomeSocket = new ServerSocket(7100);

     while ( true )
     {
        Log.d("TAG", "looking for socket");
        Socket connectionSocket = welcomeSocket.accept();
        Log.d("TAG", "socket accepted");
        Log.d("TAG", "getting input stream");
        BufferedReader inFromClient = new BufferedReader(
              new InputStreamReader(connectionSocket.getInputStream()));
        Log.d("TAG", "input stream found");
        Log.d("TAG", "getting output stream");
        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
        Log.d("TAG", "output stream found");
        Log.d("TAG", "reading input stream");
        clientSentence = inFromClient.readLine();
        Log.d("TAG", "input stream read");
        Log.d("TAG", "input = " + clientSentence);
        Log.d("TAG", "writing output back to client");
        outToClient.writeBytes(clientSentence);
        Log.d("TAG", "output written back to client");
     }
  }
  catch ( IOException e )
  {
     e.printStackTrace();
  }
}

If I initialize the TCP server first I get output 如果我首先初始化TCP服务器,则会得到输出

initTcpTestServer()
looking for socket

After initializing the TCP server and then initializing the TCP client I get from the server 初始化TCP服务器,然后初始化TCP客户端后,我从服务器获取

getting input stream
input stream found
getting output stream
output stream found
reading input stream

and from the client 和来自客户

initializing socket
socket initialized
getting output stream to server
found output stream to server
getting input stream from server
found input stream from server
writing sentence to server
sentence written
waiting for sentence response from server

so it appears that the socket is established, but the server blocks at line 因此看来套接字已建立,但是服务器在第

clientSentence = inFromClient.readLine();

and the client blocks at 而客户在

String serverResponse = inFromServer.readLine();

becuase the client has written the data, but the server has never received it, and the client is hanging waiting for the server's reponse. 因为客户端已写入数据,但是服务器从未收到过数据,并且客户端正在挂起,等待服务器的响应。

Thank you Scary Wombat. 谢谢可怕的袋熊。 Adding a "\\n" at the end of the String resulted in a successful TCP message to the server. 在字符串末尾添加“ \\ n”会导致成功向服务器发送TCP消息。 A TCP server can indeed be set up on an Android emulator by configuring port forwarding on the AVD virtual router with ADB. 通过在具有ADB的AVD虚拟路由器上配置端口转发,确实可以在Android仿真器上设置TCP服务器。 However, I have only testing this on local host. 但是,我只在本地主机上对此进行了测试。

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

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