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/CMakeLists.txt b/CMakeLists.txt index 74c2015..b5c30e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,20 +6,35 @@ 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 - client_example.cpp - server_example.cpp +add_library(${PROJECT_NAME} src/tcp_client.cpp src/tcp_server.cpp src/client.cpp) -target_link_libraries (tcp_client_server ${CMAKE_THREAD_LIBS_INIT}) +option(SERVER_EXAMPLE "Build SERVER" ON) + +if(SERVER_EXAMPLE) + + add_definitions( + -DSERVER_EXAMPLE + ) + + add_executable(tcp_server server_example.cpp) + + target_link_libraries (tcp_server ${PROJECT_NAME} ${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) + + target_link_libraries (tcp_client ${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) + +endif() diff --git a/README.md b/README.md index ef0c521..bd04482 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 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: + +```bash +$ 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: +```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.