Update Readme.md

This commit is contained in:
Kirigaya Kazuto 2018-06-11 14:39:57 +08:00 committed by GitHub
parent ea78fb096e
commit ece0028d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ Licensed under MIT
# Example # Example
## TCP Client Example ## TCP Client Example
```cpp ```cpp
#include "GSock/gsock.h" #include "GSock/gsock.h"
#include "GSock/gsock_helper.h" #include "GSock/gsock_helper.h"
@ -48,3 +49,48 @@ int main()
return 0; 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;
}
```