开发者

Better solution for Client/Server Socket-System

开发者 https://www.devze.com 2023-04-12 13:10 出处:网络
I have a Server and clients(1000) which the server communicates in every sec. I am creating a single thread on the server for each Client.

I have a Server and clients(1000) which the server communicates in every sec. I am creating a single thread on the server for each Client.

What I want to know:

Is it a good idea to use Thread for each client (Thread is created, when they loggedin succesfully) or is there another way to make more efficient to communicate between server and clients?

Server programmed with Java and the clients with C

And server specifications:

64Bit TwelveCore AMD CPU 2xAMD Opteron™ 6174,

2,2 Ghz DDR3-RAM bis 128 GB ECC, reg. (errorCorrected)

20 TB S-ATA 开发者_StackOverflow中文版II and 6 TB SAS (HotSwap) HDD

Thank You


Theoretically you should be having separate thread processing for each client's request. However practically this is not possible giving the constraint of limited hardware resources. Limit on hardware resources stops from creating unlimited number of threads and creating a separate thread for each request is a costly operation in terms of tasks to be performed by OS to manage those threads. To fix the problem you should think towards having thread-pool by which you can reuse the threads to serve different clients. If you use Java on server side then concurrency package of Java (1.5 onwards) provides the full infrastructure to create and use thread-pools.See http://download.oracle.com/javase/tutorial/essential/concurrency/pools.html for more details.


You can use non-blocking NIO to share connections between threads. The minimum number being 1 ;) If you want to use a Dispacher model, I suggest using a library which handles the details for you. e.g. netty.

In tests I have done, using 10,000 connections wastes about 1 core. If you have 1,000 I would expect you are wasting about 10% of one core. My point being that while its more efficient, it may not be worth signifciant changes to your application to support it.


I would suggest you look into Java NIO (New I/O) which you can use to make your server single threaded (at least very few threads).

Here are some nice examples:

http://www.exampledepot.com/egs/java.nio/NbServerSocket.html
http://www.exampledepot.com/egs/java.nio/AcceptSocket.html
http://www.exampledepot.com/egs/java.nio/NbServer.html
0

精彩评论

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

关注公众号