From c9e17ab41a628caac468286349f73e86e2a2daa1 Mon Sep 17 00:00:00 2001 From: doleron Date: Mon, 15 Feb 2021 10:28:57 -0300 Subject: [PATCH 1/4] build client and server --- CMakeLists.txt | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 74c2015..d36e878 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,20 +6,38 @@ find_package (Threads) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread") -# Enable SERVER_EXAMPLE or CLIENT_EXAMPLE -# to compile one of the examples. -# Each example contains a main() function, so -# enable only one example, to avoid main() collision -add_definitions( - -DSERVER_EXAMPLE -# -DCLIENT_EXAMPLE -) - -add_executable(tcp_client_server +option(SERVER_EXAMPLE "Build SERVER" ON) + +if(SERVER_EXAMPLE) + + add_definitions( + -DSERVER_EXAMPLE + ) + + add_executable(tcp_server + server_example.cpp + src/tcp_client.cpp + src/tcp_server.cpp + src/client.cpp) + + target_link_libraries (tcp_server ${CMAKE_THREAD_LIBS_INIT}) + +endif() + +option(CLIENT_EXAMPLE "Build CLIENT" ON) + +if(CLIENT_EXAMPLE) + + add_definitions( + -DCLIENT_EXAMPLE + ) + + add_executable(tcp_client client_example.cpp - server_example.cpp src/tcp_client.cpp src/tcp_server.cpp src/client.cpp) -target_link_libraries (tcp_client_server ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries (tcp_client ${CMAKE_THREAD_LIBS_INIT}) + +endif() From ed79e6813bcb0298172b9fffcad7b84b004b1c86 Mon Sep 17 00:00:00 2001 From: doleron Date: Mon, 15 Feb 2021 10:44:50 -0300 Subject: [PATCH 2/4] updating build instructions --- .gitignore | 1 + README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d6549 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/README.md b/README.md index ef0c521..a21cde4 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,78 @@ The code is documented, so I hope you find it easy to change it to suit your nee The server class supports multiple clients. -### Compilation -1. Add pthread library flag -2. To enable client example or server example, refer to line 9 in the cmake list. The server example is enabled by default. +## Building both server and client + +This project is set to use CMake to build both client example and server example. + +To build: + +```bash +$ git clone https://github.com/elhayra/tcp_server_client.git +$ cd tcp_server_client +$ mkdir build +$ cmake .. +$ make +``` +The commands above generate two files: `tcp_client` and `tcp_server`. + +To run, open a terminal, move to the `build` folder and execute: +``` +./tcp_server +``` +The server will print out something like: +```bash +Server setup succeeded +``` +In another terminal, move to `build` folder again and execute the client by: +``` +./tcp_client +``` +The client will output: +```bash +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: +```bash +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: + +```bash +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: + +```bash +cmake -DCLIENT_EXAMPLE=OFF .. +``` +And then run the make command as usual. From 43f50430fdc44173853e943e3c39df944acd1a57 Mon Sep 17 00:00:00 2001 From: doleron Date: Mon, 15 Feb 2021 11:03:53 -0300 Subject: [PATCH 3/4] separating lib code from the examples --- CMakeLists.txt | 21 +++++++++------------ README.md | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d36e878..b5c30e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,11 @@ find_package (Threads) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread") +add_library(${PROJECT_NAME} + src/tcp_client.cpp + src/tcp_server.cpp + src/client.cpp) + option(SERVER_EXAMPLE "Build SERVER" ON) if(SERVER_EXAMPLE) @@ -14,13 +19,9 @@ if(SERVER_EXAMPLE) -DSERVER_EXAMPLE ) - add_executable(tcp_server - server_example.cpp - src/tcp_client.cpp - src/tcp_server.cpp - src/client.cpp) + add_executable(tcp_server server_example.cpp) - target_link_libraries (tcp_server ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries (tcp_server ${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) endif() @@ -32,12 +33,8 @@ if(CLIENT_EXAMPLE) -DCLIENT_EXAMPLE ) - add_executable(tcp_client - client_example.cpp - src/tcp_client.cpp - src/tcp_server.cpp - src/client.cpp) + add_executable(tcp_client client_example.cpp) - target_link_libraries (tcp_client ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries (tcp_client ${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) endif() diff --git a/README.md b/README.md index a21cde4..6ee42d3 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ $ make ``` The commands above generate two files: `tcp_client` and `tcp_server`. -To run, open a terminal, move to the `build` folder and execute: +To run them, open a terminal, move to the `build` folder and execute: ``` ./tcp_server ``` From 4754a48e8421b338f501cb45ace6ace0435dc1e8 Mon Sep 17 00:00:00 2001 From: doleron Date: Mon, 15 Feb 2021 11:08:47 -0300 Subject: [PATCH 4/4] explanation about the static lib generation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6ee42d3..bd04482 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The server class supports multiple clients. ## Building both server and client -This project is set to use CMake to build both client example and server example. +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: @@ -22,9 +22,9 @@ $ mkdir build $ cmake .. $ make ``` -The commands above generate two files: `tcp_client` and `tcp_server`. +The commands above generate three files: `libtcp_client_server.a`, `tcp_client` and `tcp_server`. -To run them, open a terminal, move to the `build` folder and execute: +To run the last two, open a terminal, move to the `build` folder and execute: ``` ./tcp_server ```