按安装与配置接入 C++20 应用。先启动对应服务端,再在另一个终端运行本页客户端。
回显客户端
客户端发送一条消息,验证回显后退出。连接失败、发送未能入队,或五秒内未收到正确回显时,返回非零状态。
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <string_view>
#include <arknet/tcp/tcp_client.hpp>
int main()
{
std::promise<std::string> reply;
auto received = reply.get_future();
bool received_once = false;
arknet::tcp_client client;
client.set_auto_reconnect(false);
client.bind_recv([&](std::string_view bytes)
{
if (!received_once)
{
received_once = true;
reply.set_value(std::string(bytes));
}
});
if (!client.start("127.0.0.1", 7000, arknet::use_dgram))
{
std::cerr << arknet::get_last_error().message() << '\n';
return 1;
}
if (!client.async_send(std::string("hello TCP")))
return 1;
const bool ready = received.wait_for(std::chrono::seconds(5)) ==
std::future_status::ready;
const bool matched = ready && received.get() == "hello TCP";
client.stop();
std::cout << (matched ? "echo received\n" : "echo failed\n");
return matched ? 0 : 1;
}