Program freeze when calling Server method in Java -
i have basic gui
in java
there jbutton
,i have given functionality start server
button. when click button program freezes. because of while loop
? if how can overcome this?
server code
void connect_clients() { try { serversocket listener = new serversocket(7700); try { while (true) { socket socket = listener.accept(); try { printwriter out = new printwriter(socket.getoutputstream(), true); out.println(new date().tostring()); } { socket.close(); } } } { listener.close(); } } catch (ioexception ex) { logger.getlogger(test_frame.class.getname()).log(level.severe, null, ex); } }
your program freezing because blocking ui thread. need post on separate thread:
public void postlisten() { new thread(new runnable() { public void run() { connect_clients(); } }).start(); }
call method instead , should run connect_clients() method on separate thread. new thread block until client connects.