简体   繁体   English

本地PC服务器和android仿真器之间的套接字连接

[英]Socket connection between local PC server and android emulator

I have troubles with socket connection of PC (simple server written on Java) and android emulator. 我对PC(用Java编写的简单服务器)和android模拟器的套接字连接有麻烦。 Connection is established, server sends data but when I try to read it on android it always reads a null-string. 连接已建立,服务器发送数据,但是当我尝试在android上读取数据时,它始终读取空字符串。 Here is some parts of my code: 这是我的代码的某些部分:

Server: 服务器:

serverSocket = new ServerSocket(8888);
socket = serverSocket.accept();
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
output.write("Output string");
socket.close();

Client: 客户:

socket = new Socket("10.0.2.2", 8888);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = input.readLine();
Log.i(TAG, s);
socket.close();

I've omitted try-catches and logs for clearness. 为了清楚起见,我省略了try-catches和日志。 According to logs, connection is established, server sends data, but client receives only null-strings. 根据日志,建立连接,服务器发送数据,但客户端仅接收空字符串。 I'd appreciate any help. 我将不胜感激。

This works for me... (This code only for write and read data from socket for both server and client) 这对我有用...(此代码仅用于从套接字为服务器和客户端写入和读取数据)

Server: 服务器:

  BufferedOutputStream bos = new BufferedOutputStream(connection.
      getOutputStream());

  /** Instantiate an OutputStreamWriter object with the optional character
   * encoding.
   */
  OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

  String process = "Calling the Socket Server on "+ host + " port " + port;

  /** Write across the socket connection and flush the buffer */
  osw.write(process);
  osw.flush();

Client: 客户:

 /** Instantiate a BufferedInputStream object for reading
      /** Instantiate a BufferedInputStream object for reading
       * incoming socket streams.
       */

      BufferedInputStream bis = new BufferedInputStream(connection.
          getInputStream());
      /**Instantiate an InputStreamReader with the optional
       * character encoding.
       */

      InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

      /**Read the socket's InputStream and append to a StringBuffer */
      int c;
      while ( (c = isr.read()) != 13)
        instr.append( (char) c);

      /** Close the socket connection. */
      connection.close();
      System.out.println(instr);
     }
    catch (IOException f) {
      System.out.println("IOException: " + f);
    }
    catch (Exception g) {
      System.out.println("Exception: " + g);
    }

Hope this will help you.. 希望这个能对您有所帮助..

hi @yuriy nothing is wrong with your code actually your not writing full line to output stream so its giving error use it it worked for me 您好@yuriy您的代码没有任何问题,实际上您没有在输出流中写整行,因此使用它给了错误,对我有用

 serverSocket = new ServerSocket(8888);
    socket = serverSocket.accept();
    PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
   output.println("Output string");
    socket.close();

just replace output.write with output.println in server 只需在服务器中将output.write替换为output.println

Emulator listen his "own" local network ports. 仿真器监听其“自己的”本地网络端口。 You should call port forward from your local pc to emulator ports. 您应该将端口从本地PC转发到仿真器端口。

Read android adb port forwarding. 阅读android adb端口转发。

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

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