我尝试在Android WiFi-Direct
中实施TCP server
和phone-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
处的日志停止。
Server
与Client
之间的关联已建立。 Server
可以将Byte data
发送给客户。
但Server
无法从Client
收到数据。
我错过了什么吗?
答案 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.