-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_server_example.cpp
More file actions
53 lines (44 loc) · 1.46 KB
/
tcp_server_example.cpp
File metadata and controls
53 lines (44 loc) · 1.46 KB
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
#include "logger.h"
#include <slick/socket/tcp_server.h>
#include <iostream>
#include <string>
class TCPServer : public slick::socket::TCPServerBase<TCPServer>
{
public:
TCPServer(const slick::socket::TCPServerConfig& config)
: slick::socket::TCPServerBase<TCPServer>("Echo TCP Server", config)
{
}
void onClientConnected(int client_id, const std::string& client_address)
{
LOG_INFO("{} client connected: ID={}, Address={}", name_, client_id, client_address);
}
void onClientDisconnected(int client_id)
{
LOG_INFO("{} client disconnected: ID={}", name_, client_id);
}
void onClientData(int client_id, const uint8_t* data, size_t length)
{
std::string msg((char*)data, length);
std::cout << "Data received from client ID=" << client_id << ", " << msg << std::endl;
send_data(client_id, msg);
}
};
int main()
{
slick::socket::TCPServerConfig config;
config.port = 9090; // Set custom port if needed
config.max_connections = 50; // Set max connections
config.receive_buffer_size = 8192; // Set receive buffer size
config.connection_timeout = std::chrono::milliseconds(60000); // Set connection timeout
TCPServer server(config);
if (!server.start())
{
std::cerr << "Failed to start server." << std::endl;
return -1;
}
std::cout << "Press Enter to stop the server..." << std::endl;
std::cin.get();
server.stop();
return 0;
}