From ece0028d53cccac85c3e3e214efa2dd63f86f1e6 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Mon, 11 Jun 2018 14:39:57 +0800 Subject: [PATCH] Update Readme.md --- Readme.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Readme.md b/Readme.md index 37cabe7..19f4df8 100644 --- a/Readme.md +++ b/Readme.md @@ -7,6 +7,7 @@ Licensed under MIT # Example ## TCP Client Example + ```cpp #include "GSock/gsock.h" #include "GSock/gsock_helper.h" @@ -48,3 +49,48 @@ int main() return 0; } ``` + +## TCP Echo Server Example + +```cpp +#include "GSock/gsock.h" +#include "GSock/gsock_helper.h" + +void service_main(sock& s) +{ + char buff[1024]; + sock_helper sh(s); + + while (true) + { + memset(buff, 0, 1024); + int ret = s.recv(buff, 1024); + if (ret <= 0) break; + sh.sendall(buff, ret); + } +} + +int main() +{ + serversock t; + if (t.bind(59123) < 0 || t.listen(10) < 0) + { + printf("Failed to start up server.\n"); + return 1; + } + + while (true) + { + sock s; + if (t.accept(s) < 0) + { + printf("Failed to accept.\n"); + break; + } + + service_main(s); + } + + return 0; +} +```