简体   繁体   中英

data received on server from android app always null

I am trying to connect from a android emulator to a application on my desktop and send a line of text. My app is able to connect to the server, but when ever i try to read data its always null

Server application running on my desktop:

ServerSocket ss = new ServerSocket(9001);
Socket cs = ss.accept();
if (cs.isConnected()) {
    System.out.println("Client connected.");
}

BufferedReader reader = new BufferedReader(new InputStreamReader(cs.getInputStream()));

String str = reader.readLine();
System.out.println("Data:" + str);

Client application running in android app emulator:

    InetAddress addr = InetAddress.getByName("10.0.2.2");
    Socket socket = new Socket(addr, 9001);

    if (socket.isConnected()) {
        Log.d("APP", "socket connected");
    }

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    String str = "this is a sample";
    pw.write(str);

I can see that the isConnected function of the socket on both the client and server turns true. But the Data printed on the server is always null.

Thanks

尝试在“ pw.write(str)”语句之后添加刷新调用:

pw.flush();

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