开发者

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:

  1. A main thread containing a blocking loop waiting for clients. This main thread spawns a new Thread when a client is accepted.

  2. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜