2013年11月18日星期一

How to use socket and multi -threaded concurrent processing on the server side to achieve more than the client's request ?

I am not very familiar with socket ,
now need is :
clients can log on to the server up for data CRUD and exit a series of operations ; but multiple clients can log in with the progress , if they are concurrent operation, then the server is supposed to use multiple threads, to each customer of a separate thread , but do not know how to write the server side code to achieve independence between the various threads !
Once connected to a client , as long as it does not exit , this thread should not end !
------ Solution ---------------------------------------- ----
give you the address , go and see.
http://kanpiaoxue.iteye.com/admin/blogs/1956800
could build the above corrections.
recommend that you use netty to write your server side, it 's performance is very good .
Then you still see "Java Network Programming" book now .
------ For reference only -------------------------------------- -
I now own the whole out of a solution , more humble , Mo laughed :

main thread using a ServerSocket 's accept () method to deal with all client requests sent to all reception ;

get the socket from the receiver reads the client ip address information, which is based on ip judgment before the client has established a connection with the server , if you have not created before , then re-open a server-side thread and pass this socket , the socket to get the thread in its interior after appropriate treatment , the thread will then be stored in a Map, which is key to the ip address , the thread object value ;

next If this request is sent to the client and then , you can find this Map to deal with it directly threads to be processed ;

thread needs to wait for client information in order to continue the place to make it temporarily with a wait wait , when the main thread has received a request from this client information, then the thread wakes up (Notify);

When the client disconnects ( server side will receive an amount of label ) , the server-side thread end of the run , and remove the corresponding entry in the Map .

没有评论:

发表评论