|
| 1 | +# MultipleThreadMultipleClientSingleServerSocketCPP |
| 2 | +A single Server Handles multiple clients request using multiple threading |
| 3 | +<!-- |
| 4 | +*** Thanks for checking out the Best-README-Template. If you have a suggestion |
| 5 | +*** that would make this better, please fork the repo and create a pull request |
| 6 | +*** or simply open an issue with the tag "enhancement". |
| 7 | +*** Thanks again! Now go create something AMAZING! :D |
| 8 | +--> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<!-- PROJECT SHIELDS --> |
| 13 | +<!-- |
| 14 | +*** I'm using markdown "reference style" links for readability. |
| 15 | +*** Reference links are enclosed in brackets [ ] instead of parentheses ( ). |
| 16 | +*** See the bottom of this document for the declaration of the reference variables |
| 17 | +*** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use. |
| 18 | +*** https://www.markdownguide.org/basic-syntax/#reference-style-links |
| 19 | +
|
| 20 | +[![Contributors][contributors-shield]][contributors-url] |
| 21 | +[![Forks][forks-shield]][forks-url] |
| 22 | +[![Stargazers][stars-shield]][stars-url] |
| 23 | +[![Issues][issues-shield]][issues-url] |
| 24 | +[![MIT License][license-shield]][license-url] |
| 25 | +[![LinkedIn][linkedin-shield]][linkedin-url] |
| 26 | +--> |
| 27 | + |
| 28 | + |
| 29 | +<!-- PROJECT LOGO --> |
| 30 | +<!-- |
| 31 | +<br /> |
| 32 | +<p align="center"> |
| 33 | +
|
| 34 | + <a href="https://github.com/othneildrew/Best-README-Template"> |
| 35 | + <img src="images/logo.png" alt="Logo" width="80" height="80"> |
| 36 | + </a> |
| 37 | +<p align="center"> |
| 38 | + <h3 align="center">Best-README-Template</h3> |
| 39 | +
|
| 40 | + <p align="center"> |
| 41 | + An awesome README template to jumpstart your projects! |
| 42 | + <br /> |
| 43 | + <a href="https://github.com/othneildrew/Best-README-Template"><strong>Explore the docs »</strong></a> |
| 44 | + <br /> |
| 45 | + <br /> |
| 46 | + <a href="https://github.com/othneildrew/Best-README-Template">View Demo</a> |
| 47 | + · |
| 48 | + <a href="https://github.com/othneildrew/Best-README-Template/issues">Report Bug</a> |
| 49 | + · |
| 50 | + <a href="https://github.com/othneildrew/Best-README-Template/issues">Request Feature</a> |
| 51 | + </p> |
| 52 | +</p> |
| 53 | +--> |
| 54 | + |
| 55 | + |
| 56 | +<!-- TABLE OF CONTENTS --> |
| 57 | +<details open="open"> |
| 58 | + <summary>Table of Contents</summary> |
| 59 | + <ol> |
| 60 | + <li> |
| 61 | + <a href="#about-the-project">About The Project</a> |
| 62 | + <ul> |
| 63 | + <li><a href="#built-with">Built With</a></li> |
| 64 | + </ul> |
| 65 | + </li> |
| 66 | + <li> |
| 67 | + <a href="#getting-started">Getting Started</a> |
| 68 | + <ul> |
| 69 | + <li><a href="#prerequisites">Prerequisites</a></li> |
| 70 | + <li><a href="#installation">Installation</a></li> |
| 71 | + </ul> |
| 72 | + </li> |
| 73 | + <li><a href="#usage">Usage</a></li> |
| 74 | + <li><a href="#roadmap">Roadmap</a></li> |
| 75 | + <li><a href="#contributing">Contributing</a></li> |
| 76 | + <li><a href="#license">License</a></li> |
| 77 | + <li><a href="#contact">Contact</a></li> |
| 78 | + <li><a href="#acknowledgements">Acknowledgements</a></li> |
| 79 | + </ol> |
| 80 | +</details> |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +<!-- ABOUT THE PROJECT --> |
| 85 | +## About The Project |
| 86 | + |
| 87 | +<!-- [![Product Name Screen Shot][product-screenshot]](https://example.com) --> |
| 88 | + |
| 89 | +The primary intention of writing this project is to give an overview of how one can entertain multiple client requests to a server in parallel. |
| 90 | +In this project a TCP/IP server is created. The server can receive multiple client requests at the same time and entertain each client request |
| 91 | +in parallel so that no client will have to wait for server time. |
| 92 | + |
| 93 | +The TCP/IP server has been designed with multi-threading for parallel processing. |
| 94 | +The server itself is defined as a class. There is another class called myThread that handles the multi-threading mecanism. |
| 95 | +A special processor derived class (ProcessSingleClient) to just handle a client that sends a clock time information to the server. |
| 96 | + |
| 97 | +* First of all, the server is initialized then bound to a specific port number |
| 98 | +* Next, the server starts listenning to a new connexion |
| 99 | +* A new accepted client will get a thread allocated to it and start exhanging with the server |
| 100 | + |
| 101 | +* Parallelly the server will still be listenng to a new connexion |
| 102 | + |
| 103 | +* Logs are generated for both server and client using the boost log library |
| 104 | +* Also the command to pass the option to the main function use the boost program_options library |
| 105 | + |
| 106 | +A list of commonly used resources that I find helpful are listed in the acknowledgements. |
| 107 | + |
| 108 | +### Built With |
| 109 | + |
| 110 | + |
| 111 | +* [boost](https://boost.org/) |
| 112 | +* [cmake](https://cmake.org/) |
| 113 | +* [gnu](https://www.gnu.org/) |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +<!-- GETTING STARTED --> |
| 118 | +## Getting Started |
| 119 | + |
| 120 | +This is an sample code of how to implement a single server mutilple clients communication mutli-threading |
| 121 | +To get a local copy up and running follow these simple steps. |
| 122 | + |
| 123 | +### Prerequisites |
| 124 | + |
| 125 | +This is an example of how to list things you need to use the software and how to install them. |
| 126 | +* tree |
| 127 | + ```sh |
| 128 | + sudo apt-get install tree |
| 129 | + ``` |
| 130 | +* cmake |
| 131 | + ```sh |
| 132 | + sudo apt-get install cmake |
| 133 | + ``` |
| 134 | + * CPP standard: `CMAKE_CXX_STANDARD 17` |
| 135 | +* boost |
| 136 | + ```sh |
| 137 | + cd /opt |
| 138 | + mkdir boost |
| 139 | + sudo apt-get -y install build-essential g++ python-dev autotools-dev libicu-dev libbz2-dev |
| 140 | + wget http://downloads.sourceforge.net/project/boost/boost/1.76.0/boost_1_76_0.tar.gz |
| 141 | + tar -zxvf boost_1_76_0.tar.gz |
| 142 | + cd boost_1_76_0 |
| 143 | + # get the no of cpucores to make faster |
| 144 | + cpuCores=`cat /proc/cpuinfo | grep "cpu cores" | uniq | awk '{print $NF}'` |
| 145 | + echo "Available CPU cores: "$cpuCores |
| 146 | + ./bootstrap.sh # this will generate ./b2 |
| 147 | + sudo ./b2 --with=all -j $cpuCores install |
| 148 | + ``` |
| 149 | + * Check boost version |
| 150 | + ```sh |
| 151 | + cat /usr/local/include/boost/version.hpp | grep "BOOST_LIB_VERSION" |
| 152 | + ``` |
| 153 | + * Result |
| 154 | + ```sh |
| 155 | + // BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION |
| 156 | + #define BOOST_LIB_VERSION "1_76_0" |
| 157 | + ``` |
| 158 | + |
| 159 | +### Installation |
| 160 | + |
| 161 | +1. Clone the repo |
| 162 | + ```sh |
| 163 | + git clone https://github.com/zoumson/MultipleThreadMultipleClientSingleServerSocketCPP.git |
| 164 | + ``` |
| 165 | +2. Go to build |
| 166 | + ```sh |
| 167 | + cd ./build |
| 168 | + ``` |
| 169 | +3. Make sure build folder is empty, only .gitignore there is fine |
| 170 | +* Command |
| 171 | + ```sh |
| 172 | + tree -L 1 |
| 173 | + ``` |
| 174 | +* Expected result |
| 175 | + ```sh |
| 176 | + . |
| 177 | + |
| 178 | + 0 directories, 0 files |
| 179 | + ``` |
| 180 | +4. Then run the cmake file to generate the makefile |
| 181 | + ```sh |
| 182 | + cmake .. |
| 183 | + ``` |
| 184 | +5. Finally run the makefile to generate the executable `server` and `client` |
| 185 | + ```sh |
| 186 | + make |
| 187 | + ``` |
| 188 | + |
| 189 | + |
| 190 | +<!-- USAGE EXAMPLES --> |
| 191 | +## Usage |
| 192 | +Command line arguments |
| 193 | +<br> |
| 194 | +1. Sever side |
| 195 | +* Command parser using boost options_program |
| 196 | +``` |
| 197 | +Usage: options_description [options] |
| 198 | +Allowed options: |
| 199 | + -h [ --help ] produce help message |
| 200 | + -p [ --port ] [=arg(=60000)] (=no) server port number. |
| 201 | + -c [ --connexion ] [=arg(=10)] (=few) server max connexion. |
| 202 | +
|
| 203 | +``` |
| 204 | +* Dafaut server port number: random available port from host |
| 205 | + ```sh |
| 206 | + ./server |
| 207 | + ``` |
| 208 | +* Implicit server port number: 60000 |
| 209 | + ```sh |
| 210 | + ./server -p |
| 211 | + ``` |
| 212 | + * Self defined server port number: myPort |
| 213 | + ```sh |
| 214 | + ./server -p myPort |
| 215 | + ``` |
| 216 | + * Server log: `server.log` |
| 217 | + * Sample server log |
| 218 | + |
| 219 | + |
| 220 | + ``` |
| 221 | + [2021-05-28 02:08:11]<6>: Server IP: [192.168.1.106] |
| 222 | + [2021-05-28 02:08:11]<6>: Server max connexion: [5] |
| 223 | + [2021-05-28 02:08:11]<6>: Creating a server socket ... |
| 224 | + [2021-05-28 02:08:11]<6>: A client socket created |
| 225 | + [2021-05-28 02:08:11]<6>: Binding to a local port ... |
| 226 | + [2021-05-28 02:08:11]<6>: Bind success |
| 227 | + [2021-05-28 02:08:11]<6>: Server Port Number: [60000] |
| 228 | + [2021-05-28 02:08:11]<6>: Listenning to a new connexion... |
| 229 | + [2021-05-28 02:08:11]<6>: A client connected to a server |
| 230 | + [2021-05-28 02:08:11]<6>: Accepting a connexion... |
| 231 | + [2021-05-28 02:08:42]<6>: Connexion accepted |
| 232 | + [2021-05-28 02:08:42]<6>: Client IP: [192.168.1.106] |
| 233 | + [2021-05-28 02:08:42]<6>: Client Port Number: [49034] |
| 234 | + [2021-05-28 02:08:42]<6>: Accepting a connexion... |
| 235 | + [2021-05-28 02:08:42]<6>: Time received from the client 6: 02:08:42 |
| 236 | + [2021-05-28 02:08:48]<6>: Time received from the client 6: 02:08:48 |
| 237 | + [2021-05-28 02:08:54]<6>: Time received from the client 6: 02:08:54 |
| 238 | + [2021-05-28 02:09:00]<6>: Time received from the client 6: 02:09:00 |
| 239 | + [2021-05-28 02:09:06]<6>: Time received from the client 6: 02:09:06 |
| 240 | + [2021-05-28 02:09:12]<6>: Time received from the client 6: 02:09:12 |
| 241 | + [2021-05-28 02:09:18]<6>: Connexion accepted |
| 242 | + [2021-05-28 02:09:18]<6>: Client IP: [192.168.1.106] |
| 243 | + [2021-05-28 02:09:18]<6>: Client Port Number: [49036] |
| 244 | + [2021-05-28 02:09:18]<6>: Accepting a connexion... |
| 245 | + [2021-05-28 02:09:18]<6>: Time received from the client 7: 02:09:18 |
| 246 | + [2021-05-28 02:09:18]<6>: Time received from the client 6: 02:09:18 |
| 247 | + [2021-05-28 02:09:24]<6>: Time received from the client 7: 02:09:24 |
| 248 | + [2021-05-28 02:09:24]<6>: Time received from the client 6: 02:09:24 |
| 249 | + [2021-05-28 02:09:30]<6>: Closing connexion with client: 7 |
| 250 | + [2021-05-28 02:09:30]<6>: A Server closing an accpeted connexion ... |
| 251 | + [2021-05-28 02:09:30]<6>: Accepted connexion closed |
| 252 | + [2021-05-28 02:09:30]<6>: Time received from the client 6: 02:09:30 |
| 253 | + [2021-05-28 02:09:36]<6>: Closing connexion with client: 6 |
| 254 | + [2021-05-28 02:09:36]<6>: A Server closing an accpeted connexion ... |
| 255 | + [2021-05-28 02:09:36]<6>: Accepted connexion closed |
| 256 | + ``` |
| 257 | +2. Sever side |
| 258 | +* Command parser using boost options_program |
| 259 | +``` |
| 260 | +Usage: options_description [options] |
| 261 | +Allowed options: |
| 262 | + -h [ --help ] produce help message |
| 263 | + -i [ --ip ] arg server IP address |
| 264 | + -p [ --port ] arg server port number. |
| 265 | +
|
| 266 | +``` |
| 267 | +* Client log: `client.log` |
| 268 | +* Sample client log |
| 269 | + ``` |
| 270 | + [2021-05-28 02:08:42]<6>: A Client sending a request ... |
| 271 | + [2021-05-28 02:08:42]<6>: Request sent to a server |
| 272 | + [2021-05-28 02:08:42]<6>: A Client receiving a response ... |
| 273 | + [2021-05-28 02:08:42]<6>: Response from a server |
| 274 | + [2021-05-28 02:08:42]<6>: sucess |
| 275 | + [2021-05-28 02:08:48]<6>: A Client sending a request ... |
| 276 | + [2021-05-28 02:08:48]<6>: Request sent to a server |
| 277 | + [2021-05-28 02:08:48]<6>: A Client receiving a response ... |
| 278 | + [2021-05-28 02:08:48]<6>: Response from a server |
| 279 | + [2021-05-28 02:08:48]<6>: sucess |
| 280 | + [2021-05-28 02:08:54]<6>: A Client sending a request ... |
| 281 | + [2021-05-28 02:08:54]<6>: Request sent to a server |
| 282 | + [2021-05-28 02:08:54]<6>: A Client receiving a response ... |
| 283 | + [2021-05-28 02:08:54]<6>: Response from a server |
| 284 | + [2021-05-28 02:08:54]<6>: sucess |
| 285 | + [2021-05-28 02:09:00]<6>: A Client sending a request ... |
| 286 | + [2021-05-28 02:09:00]<6>: Request sent to a server |
| 287 | + [2021-05-28 02:09:00]<6>: A Client receiving a response ... |
| 288 | + [2021-05-28 02:09:00]<6>: Response from a server |
| 289 | + [2021-05-28 02:09:00]<6>: sucess |
| 290 | + [2021-05-28 02:09:06]<6>: A Client sending a request ... |
| 291 | + [2021-05-28 02:09:06]<6>: Request sent to a server |
| 292 | + [2021-05-28 02:09:06]<6>: A Client receiving a response ... |
| 293 | + [2021-05-28 02:09:06]<6>: Response from a server |
| 294 | + [2021-05-28 02:09:06]<6>: sucess |
| 295 | + [2021-05-28 02:09:12]<6>: A Client sending a request ... |
| 296 | + [2021-05-28 02:09:12]<6>: Request sent to a server |
| 297 | + [2021-05-28 02:09:12]<6>: A Client receiving a response ... |
| 298 | + [2021-05-28 02:09:12]<6>: Response from a server |
| 299 | + [2021-05-28 02:09:12]<6>: sucess |
| 300 | + [2021-05-28 02:09:18]<6>: A Client sending a request ... |
| 301 | + [2021-05-28 02:09:18]<6>: Request sent to a server |
| 302 | + [2021-05-28 02:09:18]<6>: A Client receiving a response ... |
| 303 | + [2021-05-28 02:09:18]<6>: Response from a server |
| 304 | + [2021-05-28 02:09:18]<6>: sucess |
| 305 | + [2021-05-28 02:09:24]<6>: A Client sending a request ... |
| 306 | + [2021-05-28 02:09:24]<6>: Request sent to a server |
| 307 | + [2021-05-28 02:09:24]<6>: A Client receiving a response ... |
| 308 | + [2021-05-28 02:09:24]<6>: Response from a server |
| 309 | + [2021-05-28 02:09:24]<6>: sucess |
| 310 | + [2021-05-28 02:09:30]<6>: A Client sending a request ... |
| 311 | + [2021-05-28 02:09:30]<6>: Request sent to a server |
| 312 | + [2021-05-28 02:09:30]<6>: A Client receiving a response ... |
| 313 | + [2021-05-28 02:09:30]<6>: Response from a server |
| 314 | + [2021-05-28 02:09:30]<6>: sucess |
| 315 | + ``` |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | +<!-- ROADMAP --> |
| 320 | +## Roadmap |
| 321 | + |
| 322 | +All the headers files are well docummented, read through the comments |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | +<!-- CONTRIBUTING --> |
| 327 | +## Contributing |
| 328 | + |
| 329 | +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. |
| 330 | + |
| 331 | +1. Fork the Project |
| 332 | +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) |
| 333 | +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) |
| 334 | +4. Push to the Branch (`git push origin feature/AmazingFeature`) |
| 335 | +5. Open a Pull Request |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | +<!-- LICENSE --> |
| 340 | +## License |
| 341 | + |
| 342 | +Distributed under the MIT License. See `LICENSE` for more information. |
| 343 | + |
| 344 | + |
| 345 | + |
| 346 | +<!-- CONTACT --> |
| 347 | +## Contact |
| 348 | + |
| 349 | +Adama Zouma - <!-- [@your_twitter](https://twitter.com/your_username) -->- stargue49@gmail.com |
| 350 | + |
| 351 | +Project Link: [https://github.com/zoumson/MultipleThreadMultipleClientSingleServerSocketCPP](https://github.com/zoumson/MultipleThreadMultipleClientSingleServerSocketCPP) |
| 352 | + |
| 353 | + |
| 354 | + |
| 355 | +<!-- ACKNOWLEDGEMENTS --> |
| 356 | +<!-- |
| 357 | +## Acknowledgements |
| 358 | +* [GitHub Emoji Cheat Sheet](https://www.webpagefx.com/tools/emoji-cheat-sheet) |
| 359 | +* [Img Shields](https://shields.io) |
| 360 | +* [Choose an Open Source License](https://choosealicense.com) |
| 361 | +* [GitHub Pages](https://pages.github.com) |
| 362 | +* [Animate.css](https://daneden.github.io/animate.css) |
| 363 | +* [Loaders.css](https://connoratherton.com/loaders) |
| 364 | +* [Slick Carousel](https://kenwheeler.github.io/slick) |
| 365 | +* [Smooth Scroll](https://github.com/cferdinandi/smooth-scroll) |
| 366 | +* [Sticky Kit](http://leafo.net/sticky-kit) |
| 367 | +* [JVectorMap](http://jvectormap.com) |
| 368 | +* [Font Awesome](https://fontawesome.com) |
| 369 | +--> |
| 370 | + |
| 371 | + |
| 372 | + |
| 373 | + |
| 374 | +<!-- MARKDOWN LINKS & IMAGES --> |
| 375 | +<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --> |
| 376 | +<!-- |
| 377 | +[contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=for-the-badge |
| 378 | +[contributors-url]: https://github.com/othneildrew/Best-README-Template/graphs/contributors |
| 379 | +[forks-shield]: https://img.shields.io/github/forks/othneildrew/Best-README-Template.svg?style=for-the-badge |
| 380 | +[forks-url]: https://github.com/othneildrew/Best-README-Template/network/members |
| 381 | +[stars-shield]: https://img.shields.io/github/stars/othneildrew/Best-README-Template.svg?style=for-the-badge |
| 382 | +[stars-url]: https://github.com/othneildrew/Best-README-Template/stargazers |
| 383 | +[issues-shield]: https://img.shields.io/github/issues/othneildrew/Best-README-Template.svg?style=for-the-badge |
| 384 | +[issues-url]: https://github.com/othneildrew/Best-README-Template/issues |
| 385 | +[license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=for-the-badge |
| 386 | +[license-url]: https://github.com/othneildrew/Best-README-Template/blob/master/LICENSE.txt |
| 387 | +[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555 |
| 388 | +[linkedin-url]: https://linkedin.com/in/othneildrew |
| 389 | +[product-screenshot]: images/screenshot.png |
| 390 | +--> |
0 commit comments