I can not add TCP/IP Server to the service, when closing (minimizing) the application, the server does not accept data. How to make the data transmitted continuously?
public int onStartCommand(Intent intent, int flags, int startId) {
myServer = new Server();
myServer.start();
return Service.START_STICKY;
}
Server
private class Server extends Thread {
private Socket clientSocket;
private ServerSocket server;
private BufferedReader in;
private BufferedWriter out;
private String LOG_TAG;
@Override
public void run() {
while (true) {
try {
server = new ServerSocket(9002);
clientSocket = server.accept();
try {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String word = in.readLine();
Log.d(LOG_TAG, "***" + word + "***");
out.write(word);
out.flush();
} finally {
System.out.println("run closed");
clientSocket.close();
in.close();
out.close();
}
} finally {
System.out.println("Server closed");
server.close();
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}
So many problems with this code. Here's a list of them:
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.