Coder Social home page Coder Social logo

tcp_server_client's Introduction

TCP server client

A thin and simple C++ TCP client server

This code was written to run on linux machines, and to compile with C++11 and older versions of C++ (this is why I chose to use pthread instead of std::thread).

Both server and client are very easy and simple to use. You can find code examples of both server and client.

The code is documented, so I hope you find it easy to change it to suit your needs if needed to.

The server class supports multiple clients.

Building both server and client

This project is set to use CMake to build both the client example and the server example. In addition, CMake builds a static library file to hold the common code to both server and client.

To build:

$ git clone https://github.com/elhayra/tcp_server_client.git
$ cd tcp_server_client
$ mkdir build
$ cmake ..
$ make

The commands above generate three files: libtcp_client_server.a, tcp_client and tcp_server.

To run the last two, open a terminal, move to the build folder and execute:

./tcp_server

The server will print out something like:

Server setup succeeded

In another terminal, move to build folder again and execute the client by:

./tcp_client

The client will output:

Client connected successfully
Got msg from server: server got this msg: hello server

Got msg from server: server got this msg: hello server

Got msg from server: server got this msg: hello server

In the server terminal you would see some messages like:

Server setup succeeded
Got client with IP: 127.0.0.1
-----------------
IP address: 127.0.0.1
Connected?: True
Socket FD: 4
Message: 
Observer1 got client msg: hello server

Observer1 got client msg: hello server

Observer1 got client msg: hello server

Press control+c in both terminals to stop the server and client apps.

Building only server or client

By default, the CMake configuration builds both server and client. However, ou can use flags if you want to build only one of the apps as follows:

Disabling the server build

To build only the client, disable the server build by replace the cmake .. call by:

cmake -DSERVER_EXAMPLE=OFF ..

And then run the make command as usual.

Disabling the client build

To build only the server, disable the client build by replace the cmake .. call by:

cmake -DCLIENT_EXAMPLE=OFF ..

And then run the make command as usual.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.