Martin
Martin

Reputation: 2863

Why the TCP Server does't receive any data from client in Android?

I try to implement the WiFi-Direct and the TCP server in Android phone-A.

The phone-A turn on the WiFi-Direct and connect with the phone-B , the phone-A also get the Group Owner IP address.

And I download the Socket Protocol App at phone-B as a Client. It connect to the Group Owner IP address.

The client can receive the data from Server , but the Server can not receive the data from Client.

The code of the Server is like the following:

private static Socket socket;
private static ServerSocket serverSocket;
public static final int PORT = 50006;

        public static class FileServerAsyncTask extends AsyncTask<Void, Void, Socket> {

                private Context context;
                private TextView statusText;
                private BufferedReader input_test;

                public FileServerAsyncTask(Context context, View statusText) {
                    this.context = context;
                    this.statusText = (TextView) statusText;
                }
                @Override
                protected Socket doInBackground(Void... params) {
                    try {
                        serverSocket = new ServerSocket(PORT);
                        socket = serverSocket.accept();

                        CommunicationThread comThread = new CommunicationThread(socket);
                        new Thread(comThread).start();

                        return socket;
                    } catch (IOException e) {
                        Log.e(WifiP2P.TAG, e.getMessage());
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(Socket socket) {

                }
            }


    public static final class CommunicationThread implements Runnable {

            private Socket ClientSocket;
            private BufferedReader input;

            public CommunicationThread(Socket clientsocket){
                this.ClientSocket = clientsocket;
                Log.d(WifiP2P.TAG, "CommunicationThread , Client address is " + this.ClientSocket.getInetAddress());

            }

            @Override
            public void run() {
                // TODO Auto-generated method stub

                while (!Thread.currentThread().isInterrupted()) {

                    try {
                        Log.e(WifiP2P.TAG, "@@@@ while loop ");
                        this.input = new BufferedReader(new InputStreamReader(this.ClientSocket.getInputStream()));
                        String data = input.readLine();
                        Log.e(WifiP2P.TAG, "@@@@ Client Data = " + data);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }

The log stop at the @@@@ while loop. The connection between Server and the Client is established. The Server can send the Byte data to Client.

But the Server can not receive the data from Client.

Did I missing something ?

Upvotes: 0

Views: 417

Answers (1)

onof
onof

Reputation: 17367

Do you send a line feed from the client?

public String readLine()
                throws IOException
Reads a line of text. A line is considered to be terminated by any one 
of a line feed ('\n'), a carriage return ('\r'), or a carriage 
return followed immediately by a linefeed.

Upvotes: 1

Related Questions