Its a traficklight programm. the GUI is a server. on GUI i used a button to start the server but my gui is freezing because it waiting for clients. The cliens are just recivers. i know i should use threads but i dont know how.please help me
public class Controller {
public TextArea meldingPlass;
private static int portNr = 5555;
public void startServer() throws IOException {
Server cs = new Server();
cs.coectToServer(portNr);
}
}
//Here my SeverThreads class
public class ServerThread extends Thread {
Socket s;
InetAddress ca;
public ServerThread(Socket s)
{
this.s=s;
ca=s.getInetAddress();
}
public void run()
{
try(
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
)
{
while (true){
System.out.println(ca.getHostAddress());
out.println("Status of Server");
}
// s.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
//here is ServerClass public class Server implements Ruable {
public void coectToServer(int portNr){
try(
ServerSocket ss = new ServerSocket(portNr);
)
{
while (true)
{
System.out.println("In Server - while loop");
ServerThread st = new ServerThread(ss.accept());
System.out.println("Client coected. Starting client");
st.start();
}
}
catch (Exception e){
System.out.println("Exception occurred when trying to listen on port "
+portNr+ " or listening for a coection");
System.out.println(e.getMessage());
}
}
@Override
public void run() {
}
}
