how to make the server accept new connection while processing other things in java
I am from Ethiopia so i am sorry for my English
I was designing a muiltithread server/client application and the server has its own user interface that keep the list of all connected client the problem is
ss = new ServerSocket( port );
while (true) {
// Grab the next incoming connection
s = ss.accept();
ss.accept(); bl开发者_StackOverflow社区ock the entire process and i am not able to access other functionalities of the server. the user interface of the server completly freez even though the server is accepting new clients and the client side is working perfectly.
so what shall i do to make the server accept new connection while processing other things
There are two (popular) ways of doing this:
A main thread containing a blocking loop waiting for clients. This main thread spawns a
new Thread
when a client isaccept
ed.Java New Input/Output. Everything is handled in the main thread. See http://rox-xmlrpc.sourceforge.net/niotut/index.html
Also: you point out your server application has a User Interface. A User Interface should be running in a different thread (the Event Dispatcher Thread, EDT) than the server.
you should spawn thread and call accept
there
So Put this Listener ( in a method) and use TaskFactory. TaskFactory will execute the method in a separate thread and you will not have this problem
精彩评论