-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtcp-server.cpp
59 lines (48 loc) · 1.82 KB
/
tcp-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "tcpserver.hpp"
#include <iostream>
using namespace std;
int main()
{
// Initialize server socket..
TCPServer<> tcpServer;
// When a new client connected:
tcpServer.onNewConnection = [&](TCPSocket<> *newClient) {
cout << "New client: [";
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << "]" << endl;
newClient->onMessageReceived = [newClient](string message) {
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << " => " << message << endl;
newClient->Send("OK!");
};
// If you want to use raw bytes
/*
newClient->onRawMessageReceived = [newClient](const char* message, int length) {
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << " => " << message << "(" << length << ")" << endl;
newClient->Send("OK!");
};
*/
newClient->onSocketClosed = [newClient](int errorCode) {
cout << "Socket closed:" << newClient->remoteAddress() << ":" << newClient->remotePort() << " -> " << errorCode << endl;
cout << flush;
};
};
// Bind the server to a port.
tcpServer.Bind(8888, [](int errorCode, string errorMessage) {
// BINDING FAILED:
cout << errorCode << " : " << errorMessage << endl;
});
// Start Listening the server.
tcpServer.Listen([](int errorCode, string errorMessage) {
// LISTENING FAILED:
cout << errorCode << " : " << errorMessage << endl;
});
// You should do an input loop, so the program won't terminate immediately
string input;
getline(cin, input);
while (input != "exit")
{
getline(cin, input);
}
// Close the server before exiting the program.
tcpServer.Close();
return 0;
}