Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

leancloud/cpp-socket-demo

Repository files navigation

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

About

A demo for LeanEngine C++ Runtime and socket features

Topics

Resources

Stars

Watchers

Forks

AltStyle によって変換されたページ (->オリジナル) /