Skip to content

Commit

Permalink
Merge pull request #5 from doleron/building_both_server_and_client
Browse files Browse the repository at this point in the history
Building client and server executables together
  • Loading branch information
elhayra authored Jul 17, 2021
2 parents 5524c7e + 4754a48 commit 68c52d6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
41 changes: 28 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
78 changes: 75 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

1 comment on commit 68c52d6

@ibychance
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_minimum_required(VERSION 3.13)
project(tcp_client_server)

find_package (Threads)

set(CMAKE_CXX_STANDARD 11)

add_library(mylib STATIC
src/tcp_client.cpp
src/tcp_server.cpp
src/client.cpp)

target_link_directories(mylib PUBLIC include)

add_executable(tcp_client client_example.cpp)
add_executable(tcp_server server_example.cpp)

target_link_libraries (tcp_client mylib ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (tcp_server mylib ${CMAKE_THREAD_LIBS_INIT})

Try this ....

Please sign in to comment.