我需要帮助,我在使用TCP服务器发送数据时遇到问题。我可以通过客户端将其发送到服务器。但是无法快速接收通过客户端发送到服务器的数据。我们必须首先关闭发送方的套接字连接,然后才能接收数据。有人可以帮助我解决这个问题吗?非常感谢你
代码:
class TCPServer extends AsyncTask<String,String,String> {
public String message;
private DataInputStream stream;
private ServerSocket socket;
private PrintWriter writer;
private OutputStream outputStream;
private BufferedReader reader;
@Override
protected String doInBackground(String... strings) {
try {
socket=new ServerSocket(4000);
Socket socket1=socket.accept();
this.reader=new BufferedReader(new InputStreamReader(socket1.getInputStream()));
this.outputStream=socket1.getOutputStream();
this.writer=new PrintWriter(outputStream,true);
writer.write("Hellow this is from server to client");
writer.flush();
while(true){
this.message=reader.readLine();
Log.i("information message",message);
}
} catch(UnknownHostException e){
e.printStackTrace();
Log.e("error in here",e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("error in here",e.toString());
}
return message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getBaseContext(),"Wait A minute",Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getBaseContext(),s.toString(), Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Toast.makeText(getBaseContext(),values[0],Toast.LENGTH_LONG).show();
}
}
完成了!谢谢任何人的帮助,我只是使用套接字和服务器套接字进行接收,我只是使用线程,可以将其传输到其他设备,谢谢