简体   繁体   中英

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.

I use a simple Java client program to try to connect to the server running on the emulator. I attempt to send a simple string like "hello world".

I think the connection between the client and server is successfully initialized; however, data is not routed to the Android device.

The server thread blocks at line clientSentence = inFromClient.readLine(); and the client thread blocks at String serverResponse = inFromServer.readLine(); .

I have port forwarded local host port 6100 to AVD virtual port 7100 as per Google docs with ADB

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

Here is Java class 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()

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

initTcpTestServer()
looking for socket

After initializing the TCP server and then initializing the TCP client I get from the server

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. A TCP server can indeed be set up on an Android emulator by configuring port forwarding on the AVD virtual router with ADB. However, I have only testing this on local host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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