Your application is stopping to respond because it's not threaded. Without multithreading your application will 'stop' the moment you click your "link" button because the call to ServerSocket#accept.
The reason for that is the blocking nature of the accept method. Until another socket connects the ServerSocket is waiting and keeps his current thread 'suspended'. Think of it as an inner while loop similiar to this:
while(!isConnected())
{
// check if there is a new request
}
Imagine no client would ever connect. How would this loop ever stop? Never.
A possible workaround is to either thread the entire UI part or 'data model' part.
My prefered way is to create a custom class which only handles the ServerSocket#accept method and delegates incoming connections to another thread which takes care of the rest. I do that to keep my server accessible at any time which might not be the best approach for your problem.
This is how my helper class commonly looks like:
ClientAccepter.java:
public class ClientAccepter
implements Runnable
{
// the ServerSocket
private final ServerSocket server;
// ExecutorServices ease the pain of threading your application quite a lot
private final ExecutorService es = Executors.newCachedThreadPool();
private boolean isAlive = true;
public ClientAccepter( ServerSocket server )
{
this.server = server;
}
@Override
public void run()
{
//This is where you specify your desired behaviour.
//Example:
while ( isAlive )
{
try
{
es.execute( new ClientHandler( server.accept() ) );
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
}
The call to such a class could look like this:
Server.java:
public class Server
{
//ExecutorServices are the best and easy to use.
private static ExecutorService serverThreader = Executors.newSingleThreadExecutor();
public static void main( String[] args )
{
try
{
serverThreader.execute( new ClientAccepter( new ServerSocket( 8080 ) ) );
}
catch ( IOException e )
{
e.printStackTrace();
//further exception handling.
}
}
}
To be fair this might not be the best approach but it's my preferred way of handling blocking operations. You should consider working through the orcale concurrency tutorial and definitly take a look at ExecutorServices because they are an incredible framework to handle threadpools with ease.
Also specificly for JavaFX there is a concurrency tutorial.
linkAndroid()and if you connect any device during that time. Because on the first glance eveything looks just fine.statusLbl.setText("Server started");based upon the posted code you will get a null pointer exception hereServerSocket#acceptbecause of it beeing a blocking operation until a connection has been established.