Giter VIP home page Giter VIP logo

tcp_server_client's Introduction

cmake workflow

TCP server client

A thin and simple C++ TCP client and server library with examples.

Platforms Support

Currently, both linux and mac are supported

Examples

Simple tcp server-client examples. They are optimized for simplicity and ease of use/read but not for performance. However, I believe tuning this code to suite your needs should be easy in most cases. You can find code examples of both server and client under the 'examples' directory. Both the library and the examples are well documented.

Server

The server is thread-safe, and can handle multiple clients at the same time, and remove dead clients resources automatically.

Quick start

build the examples and static library file:

$ cd tcp_server_client
$ ./build

run the server and client examples: Navigate into the build folder and run in the terminal:

$ cd build

In terminal #1:

$ ./tcp_server

In terminal #2:

$ ./tcp_client

Building

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.

In order to build the project you can either use the build.sh script:

$ cd tcp_server_client
$ ./build

or build it manually:

$ cd tcp_server_client
$ mkdir build
$ cmake ..
$ make

The build process generate three files: libtcp_client_server.a, tcp_client and tcp_server. The last two are the executables of the examples which can be executed in the terminal.

Building Only Server or Client

By default, the CMake configuration builds both server and client. However, you can use flags 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.

Run the Examples

Navigate into the build folder and run in the terminal:

$ ./tcp_server

You should see a menu output on the screen, we'll get back to that. In a different terminal, run the client:

$ ./tcp_client

You should see a similar menu for the client too. In addition, as mentioned, the client will try to connect to the server right away, so you should also see an output messages on both client and server terminals indicating that the connection succeeded. Now, feel free to play with each of the client/server menus. You can exchange messages b/w client and server, print active clients etc. You can also spawn more clients in other terminals to see how the server handles multiple clients.

Server-Client API

After playing with the examples, go into the examples source code and have a look at the main() function to learn how the server and client interacts. The examples are heavily documented. In addition, you can also look at the public functions in tcp_client.h and tcp_server.h to learn what APIs are available.

Server and Client Events

Both server and client are using the observer design pattern to register and handle events. When registering to an event with a callback, you should make sure that:

  • The callback is fast (not doing any heavy lifting tasks) because those callbacks are called from the context of the server or client.
  • No server / client function calls are made in those callbacks to avoid possible deadlock.

tcp_server_client's People

Contributors

doleron avatar elhayra avatar elhayrobotican avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tcp_server_client's Issues

Copy right license

Hello Team,

I tested your code and would like to enhance it further for my professional requirements.

Is it possible to get the license file or copy rights file to use the source code? Thank you.

automatic client reconnect

Tried this client code for automatic reconnect, but fails.
State isConnected() seems to be detected incorrectly.

while (1) {
if (client.isConnected()) {
std::cout << "Client connected successfully\n";
// send()
}
else {
std::cout << "connect ... \n";
client.connectTo("127.0.0.1", 3000);
}
sleep(1);
};

Would be glad to hear about your expert recommendation?

Segfault on a Client destruction (server side)

The server crash at random moments when receiving data from several clients.
By running the debugger I found that it appears in the TcpServer::deleteClient() method. When a Client is erased from the clients vector the Client destructor is called. Then a segfault appears when the program tries to detach() the thread.

I believe it happens because the clients vector is modified by each thread when a client end the connection. I think a mutex could solve this issue to avoid changing the vector in several threads at the same time.

bug: msg size wrong

There seems to be a bug in the server example about incoming msg size.

To reproduce:

  1. build master
  2. start client_example and server_example
  3. client: send message '1234356'
  4. server receives '123456'
  5. client send message ' 4'
  6. server receives '423456', but should be '4' only

seems to be a bug in message size

Changing the IP address of the server does not work

I found a problem on my side of the test.
For example, I changed the server's WantIP and found that the changed IP address doesn't work.
Client still needs to use 127.0.0.1 to connect to server.
Why is this?

Server `acceptClient` timeout parameter not used

It seems the int timeout parameter in:

Client TcpServer::acceptClient(uint timeout)

has no effect because it is not used to setup the timevalstruct.

It turns out the actual timeout is always 2 secs:

struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;

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.