开发者

Socket Programming Data Server

I am writing code in C++ for a data server in Linux. I have a socket list in the form of file descriptors for al开发者_开发百科l the subscriber connections. My server need to continuously send data to the subscriber. I have about 100 to 200 subscriber at max. What is the fastest way to push data to all the subscribers simultaneously? I need the data to reach the subscriber as soon as possible. I am thinking of some asynchronous methods but not sure how should I implement that. Should I just call the system asynchronous method? Or should I create multiple threads to handle each of the subscriber? Or should I implement some sort of thread pool?

Thanks.


It probably depends a lot on your system (Single CPU?) and the work load (is it IO bound, memory bound or CPU bound?). Your best bet therefore is to implement something and benchmark it. If you want to exploit multiple processors and you're mostly CPU bound then having a small thread pool (but not one per connection) is a good idea. If you're memory bound and keeping the memory footprint small is important then probably using a single thread is a good idea. If you're IO bound do whatever is easiest for you to implement since it won't make any difference in the grand scheme of things, but your time is still valuable.

For the record boost::asio is a nice, portable way of doing asynchronous comms in C++.

Also it's not entirely clear from the question, but multicast might be appropriate here, which would shift almost all of the network overhead off the server. Provided it's the same data you're sending to all subscribers this would be well worth looking at.


I don't know Linux well but do not use one thread per connection. Use whatever async method allows you to handle read/write results in an event-driven (callback) way, if such exists.


The "Advanced Programming in the Unix Environment" by W. Richard Stevens (classic book) describes ways to do advanced IO with multiplexing among multiple file descriptors (section 12.5.1 on pg. 396). Yes, I have the book in front of me. :)

I think there are 2 ways of doing this:

  1. allocating a thread pool and delegate n file descriptors to be processed by each thread.
  2. do all this in a single process using asynchronous IO using select and poll syscalls.

Now, this depends on how much data you want to push out to the clients. I'd suggest going with option 2 first and then explore 1 if 2 doesn't satisfy.


check

epoll
for multiplexing . This is much more advanced than select and lots of drawbacks of select have been covered in it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜