tags:

views:

82

answers:

2
+3  Q: 

Client and server

Hello,

I would like to create a connection between two applications. Should I be using Client-Server or is there another way of efficiently communicating between one another? Is there any premade C++ networking client server libraries which are easy to use/reuse and implement?

Application #1 <---> (Client) <---> (Server) <---> Application #2

Thanks!

+4  A: 

Client / server is a generic architecture pattern (much like factory, delegation, inheritance, bridge are design patterns). What you probably want is a library to eliminate the tedium of packing and unpacking your data in a format that can be sent over the wire. I strongly recommend you take a look at the protocol buffers library, which is used extensively at Google and released as open source. It will automatically encode / decode data, and it makes it possible for programs written in different languages to send and receive messages of the same type with all the dirty work done for you automatically. Protobuf only deals with encoding, not actually sending and receiving. For that, you can use primitive sockets (strongly recommend against that) or the Boost.Asio asynchronous I/O library.

I should add that you seem to be confused about the meaning of client and server, since in your diagram you have the application talking to a client which talks to a server which talks to another application. This is wrong. Your application is the client (or the server). Client / server is simply a role that your application takes on during the communication. An application is considered to be a client when it initiates a connection or a request, while an application is considered to be a server when it waits for and processes incoming requests. Client / server are simply terms to describe application behavior.

Michael Aaron Safyan
+1 for Boost.Asio, it's a fantastic library
Sam Miller
+1  A: 

If you know the applications will be running on the same machine, you can use sockets, message queues, pipes, or shared memory. Which option you choose depends on a lot of factors.

There is a ton of example code for any of these strategies as well as libraries that will abstract away a lot of the details.

If they are running on different machines, you will want to communicate through sockets.

There's a tutorial here, with decent code samples.

bstpierre
another consideration is how the client can see the server. If the server is across the internet, you'll likely want a web service or wrap the comm in http
seand
...except for all the cases where http would be totally inappropriate.
bstpierre
@bstpierre, where would http be inappropriate? A key advantage to HTTP is that such traffic is almost always supported. Lots of routers and firewalls will drop or block legitimate traffic if it is not HTTP traffic.
Michael Aaron Safyan
@Michael: Multicast. A/V transport. All the cases where UDP/connectionless would be more suitable.
bstpierre
@bstpierre, most routers do not support multicast packets. In fact, an HTTP based protocol such as PubSubHubbub would be a much better form of delivery. I will grant, though, that there are standard non-HTTP protocols for A/V based in UDP, and that is one rare case where HTTP is a poor choice. However, for anything but streaming media and gaming, HTTP is typically the best choice available.
Michael Aaron Safyan
@Michael: I agree that across the internet http is often a good choice. But without knowing more about the OPs application I would not make a blanket "use http" recommendation.
bstpierre