-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_server.cpp
More file actions
39 lines (31 loc) · 1.19 KB
/
udp_server.cpp
File metadata and controls
39 lines (31 loc) · 1.19 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
#include <print>
#include <span>
#include <string_view>
#include "socket.h"
struct MsgHeader {
uint32_t type;
uint32_t seq;
};
int main(int argc, char const* argv[]) {
SocketUdpReceiver<> server;
if (!server.init("", "127.0.0.1", 1234)) {
std::println("init failed: {}", server.getLastError());
return 1;
}
while (true) {
server.recvfrom([](const uint8_t* data, uint32_t size, auto addr) {
// 1. 直接打印收到的原始数据 dat
for (auto b : std::span(data, size)) {
std::print("{:02x} ", b);
}
std::println("from [{}:{}]", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
// 2. 解析时也直接使用 data
auto req_header = reinterpret_cast<const MsgHeader*>(data);
// 注意类型转换,data 是 uint8_t*,计算偏移时很安全
const char* body_ptr = reinterpret_cast<const char*>(data + sizeof(MsgHeader));
auto body_len = size - sizeof(MsgHeader);
auto body = std::string_view(body_ptr, body_len);
std::println("recv {}, type={}, seq={}", body, req_header->type, req_header->seq);
});
}
}