Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client and server

Tags:

c++

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!

like image 842
user303907 Avatar asked Feb 27 '23 13:02

user303907


2 Answers

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.

like image 120
Michael Aaron Safyan Avatar answered Mar 03 '23 11:03

Michael Aaron Safyan


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.

like image 32
bstpierre Avatar answered Mar 03 '23 12:03

bstpierre