I'm putting together a client/server application written in C/C++. It is mostly C with some C++ features - I come from the C and Java world and don't use lots of heavy advanced C++ language features. In any case, the server resides on one computer and does a variety of internal calculation, and several times a day (at unpredictable times) it will broadcast information to some number of clients (residing on other computers) that have registered with the server to listen for such broadcast messages. The server must be able to broadcast to all registered clients with 100% reliability and very quickly so that clients can update their own internal data to reflect the state change in the server - the state change that the server just broadcast to them. Since it must be reliable, it would have to be TCP/IP, not UDP.
This seems like a pretty standard architecture for networking in C++, but I'd like to find a good library that would allow me to do this easily (some sort of wrapper for the WinSock library so that I can do it easily on Windows without having to dig in to the specific quirks of WinSock) and a simple e开发者_C百科xample of how one would do this sort of thing.
Thanks.
Check out boost::asio
. Their examples are available here: http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/examples.html
It abstracts most platform-specific quirks so you can get down to business quickly.
There are two libraries for C/C++ network communications from free and open-source world that rock - LibEvent and Boost.Asio. Using them you won't need to dig into specifics of the operating system, or specific APIs for network communications. They both have a good documentation and a set of decent examples.
As a side note, even using TCP/IP doesn't make the communication protocol reliable from the higher point of view. In order to provide reliability, applications usually define higher-level protocols on top of TCP/IP. Those protocols describe sequencing of the messages, heartbeats, retransmission support etcetera. There are also reliable protocols built on top of UDP (so-called RUDP).
If you feel like speed and reliability is very important, you may want to use one of the industry leading technologies, such as Tibco, LBM (Informatica/29West), or Tervela. All of those solutions can work on top of different communication protocols while providing seamless API for the user.
This will require a decent amount of C++ features (templates, etc) but boost::asio is excellent and exactly what you need for this task. However, it'll clearly require some getting used to, as the programming model is quite different from straight C++. There is pretty good documentation and lots of examples at the site.
精彩评论