Issue
I am working on a JavaFX music player which can communicate with an Android App via TCP Sockets, displaying some Data from the music player (e.g.: Playlist). The Android app loads a list of all available music players from a database. In this list there is also the the current IP and the current PUBLIC_PORT of the music player. Then the Android app can connect to the player over the TCP socket and the PUBLIC_PORT. After that the music player returns a "PRIVATE_PORT" to the Android app (so that the app can communicate over the PRIVATE_PORT with the player), and starts listening for new clients to connect.
public void connectToServer() {
boolean end = false;
for(int i = 0; end == false; i++) {
try {
if(i > 100) {
end = true;
}
socket = new java.net.Socket(serveraddress,port);
end = true;
port = Integer.parseInt(getPortFromServer());
Log.d("", "Connected to engine on port" + port);
socket.close();
socket = new java.net.Socket(serveraddress,port);
readPlaylist();
Thread.sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Because i think this is not a very good solution, because i need alot of TCP ports i was wondering if there is any better method to implement this.
My only idea for a different implementation is a RESTful webservice, but i am sure if this would work, because i am using JavaFX and i need an executable file to launch the music player without installing a Webserver.
EDIT: Another idea would be putting a Webserver between the Android app and the JavaFX player. This way i would solve the PORT problem.
Solution
Solved the problem by using UDP Sockets.
Answered By - zFr3eak
Answer Checked By - Cary Denson (JavaFixing Admin)