简体   繁体   中英

Android connects to java server socket, but server socket is not receiving data

The Android app is connecting to a java server socket in my private network. After that, the app writes a string to the PrintWriter, but on the server side nothing is received.

I already tested the code on the same machine and it gets connected (as the app) plus it receives the string which is sent via PrintWriter.

Android app:

class SocketConnector extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            Socket socket = new Socket("192.168.2.116", Constant.HUB_PORT);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


            JSONObject jsonObject = new JSONObject();
            jsonObject.put("command", "some_command");
            jsonObject.put("value", "some_value");

            out.println(jsonObject.toString());
            Log.v("json", jsonObject.toString());

            Log.v("Echo: ", in.readLine());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (NetworkErrorException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Java server:

while (true) {

    //Open socket
    ServerSocket serverSocket = new ServerSocket(Constant.SOCKET_PORT);

    //wait for client connection
    Socket clientSocket = serverSocket.accept();

    //create input/output streams
    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    System.out.println("Connected");

    String jsonString;

    while ((jsonString = in.readLine()) != null) {
        System.out.println(jsonString);
    }


    System.out.println(jsonString);

    //pass command and value to handleCommand method
    JSONObject jsonObject = new JSONObject(jsonString);
    JSONObject response = handleCommand(jsonObject.getString(Constant.JSON_COMMAND), jsonObject.getString(Constant.JSON_VALUE));

    out.println(response.toString());

    serverSocket.close();
    clientSocket.close();
}

}

I expect that the android app sends the data correctly or the java server socket receives the sent data properly.

Couldn't stop thinking about this, because it seems so strange. I have an explanation, though, so I will take a stab at the answer:

You have two client connections reaching the server. Connection 1 connects and doesn't send anything (reason unknown). Connection 2 connects and sends the data payload. The server never even attempts to read that payload, however, because it's stuck dutifully waiting for connection 1 to send something (which it never does). The second socket is never even accept() -ed.

Naturally, when you examine the server with a debugger, you see it waiting on a readLine() . Your mistake was assuming that readLine() was associated with connection 2.

This jives with the .pcap, by the way; even if a client connection isn't accept() -ed, it still gets an ACK, and it is free to send further data to the server. Such data will also be ACK'd. After all, the server OS is in receipt of the data; your server program simply hasn't asked for it yet.

There are any number of reasons why this might work when you do a localhost test; most likely, it's timing-related.

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