java - FXML controller and Threads -
so, i'm coding college assignment , got stuck in part have build thread socketserver open new threads each client connection. here's java code:
this method linked setonaction of button:
void openserver(){ new serverthread().startthread(); }
this method called method above:
public class serverthread implements runnable{ private serversocket serv; @override public void run(){ try { serv = new serversocket(1234); while(true){ socket sc = serv.accept(); system.out.println("someone connected!"); thread t = new thread(new clientthread(sc)); t.start(); } } catch (ioexception e) { system.out.println("erro no servidor"); } } void startthread(){ thread t = new thread(new serverthread()); t.run(); } }
this thread called code above:
public class clientthread implements runnable{ private socket sock; @override public void run() { try { bufferedreader in = new bufferedreader(new inputstreamreader(sock.getinputstream())); while(true){ int cmd = in.read(); if(cmd == 1){ //register } else if (cmd == 2){ //login } } } catch (ioexception e) { system.out.println("erro no socket"); } } public clientthread(socket s){ this.sock = s; } }
the problem is: when button clicked, application stop responding, if application , socketserver running @ same thread, want know happening , fix it.
the mistake @ startthread()
method, call thread should t.start()
wrote t.run()
. yes dumb, @james_d in comments.
Comments
Post a Comment