I've read a lot on this subject but I can't seem to find some answer that could help me. I've modeled my application as frontend-backend. The backend is just a server that waits for incoming connections. The problem is I start the server as soon as the app starts and I no longer communicate with it. Now I need the server to communicate with the frontend telling it someone connected. I tryed using static methods but I get an error from being unable to update de UI from a different thead. How can I proceed? EDIT:
My server class
public class Server {
public static int uniqueID;
private final int port;
private final Boolean keepWorking;
private final String username;
public Server(int port, String username) {
this.port = port;
this.username = username;
keepWorking = true;
}
public void Start() {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (keepWorking) {
Socket socket = serverSocket.accept();
MainActivity.SomeoneConnected();
}
serverSocket.close();
} catch (IOException e) {
Log.d("Error", e.getMessage().toString());
}
}
}
That's everything I need. The server to tell the frontend someone connected