Code Monkey home page Code Monkey logo

cpp-socket-demo's Introduction

C++ Socket Demo

  • (tcp-game.cpp) A multithreading TCP game server listening on port 4000.
  • (udp-echo.cpp) A UDP echo server listening on port 4000.
  • (http.cpp) A multithreading HTTP Server on port 3000, with 0 - 999ms random delay.
  • Graceful shutdown for TCP and HTTP.

Other versions:

Build and start server

$ make && ./cpp-socket
[UDP] Socket is listening on 4000
[TCP] Socket is listening on 4000
[HTTP] Socket is listening on 3000

TCP Game

Establish two connections to server.

connection #1:

$ nc 127.0.0.1 4000
>> 1 players online, last number is 1, you must send the next number but skip numbers which multiples of 3 or includes the digit 3.
2   (your input)
>> 2 players online, 127.0.0.1:59385 sent 2.
>> 127.0.0.1:59897 lost, 2 players online.
4   (your input)
>> 1 players online, 127.0.0.1:59385 sent 4.

connection #2:

$ nc 127.0.0.1 4000
>> 2 players online, last number is 1, you must send the next number but skip numbers which multiples of 3 or includes the digit 3.
>> 2 players online, 127.0.0.1:59385 sent 2.
3   (your input)
>> you are lost
(disconnected)

UDP Echo

$ nc -u 127.0.0.1 4000
hello                   (your input)
hello                   (response)

HTTP

$ curl http://127.0.0.1:3000
Hello

Graceful shutdown

You can send SIGTERM or SIGINT (ctrl-C) to start graceful shutdown:

$ ./cpp-socket
[UDP] Socket is listening on 4000
[TCP] Socket is listening on 4000
[HTTP] Socket is listening on 3000
[TCP] A connection is accepted now.
[TCP] 127.0.0.1 connected...
(ctrl-C)
[SIGNAL] 2 workers are running, waiting for them finish ...
(wait for all connections disconnect)
[TCP] 127.0.0.1 disconnected.
(exited)

Documentation

cpp-socket-demo's People

Contributors

jysperm avatar

Watchers

 avatar  avatar  avatar

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.