为什么TCP服务器不能从Android客户端接收任何数据?

时间:2014-10-16 07:15:33

标签: android sockets tcp

我尝试在Android WiFi-Direct中实施TCP serverphone-A

phone-A启用WiFi-Direct并与phone-B关联,phone-A也获得Group Owner IP address < / p>

我在Socket Protocol App下载phone-B作为Client。它连接到Group Owner IP address

client可以从Server接收数据,但Server无法从Client接收数据。

Server的代码如下所示:

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
                    }
                }
            }
        }

@@@@ while loop处的日志停止。 ServerClient之间的关联已建立。 Server可以将Byte data发送给客户。

Server无法从Client收到数据。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您是否从客户端发送换行符?

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.