如何使用标准库网络 TS 在服务器和客户端之间发送消息C++
How to Send messages between server and client using C++ standard library networking TS
我已经尝试按照boost中的教程进行操作,但是API并不相同,所以我不得不猜测某些部分。
到目前为止,我的尝试如下所示:
#include <iostream>
#include <experimental/internet>
#include <experimental/socket>
#include <thread>
#include <chrono>
using namespace std::experimental;
int main(int argc, char* argv[])
{
std::thread server = std::thread([]()
{
std::cout << "Starting server" << std::endl;
net::io_context context;
net::ip::tcp::endpoint endpoint{net::ip::tcp::v4(), 1234};
net::ip::tcp::acceptor acceptor{context, endpoint};
acceptor.non_blocking(true);
std::cout << "opened server on " << endpoint << std::endl;
std::error_code error;
net::ip::tcp::socket socket(context);
while (true)
{
socket = acceptor.accept(error); //accept connections
if (!error) //if connected with a client
{
std::cout << "Connected to client!" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
std::string data = "Hello World!";
net::const_buffer buf(&data, sizeof(data));
socket.send(buf);
std::cout << "Sent data!" << std::endl;
while(true) {}
}
}
});
std::thread client = std::thread([]()
{
net::io_context context;
net::ip::tcp::socket socket(context);
net::ip::tcp::endpoint server{net::ip::tcp::v4(), 1234};
std::error_code error;
while(true)
{
socket.connect(server, error); //attempt to connect
if (!error) //if connected
{
std::cout << "Connected to server!" << std::endl;
net::mutable_buffer buf;
while(buf.size() == 0)
{
socket.receive(buf);
}
std::cout << "Received data!" << std::endl;
std::cout << buf.data() << std::endl;
while(true) {}
}
}
});
server.join();
return 0;
}
服务器和客户端连接,但客户端未收到消息。上面程序的输出是:
Starting server
opened server on 0.0.0.0:1234
Connected to server!
Connected to client!
Sent data!
然后它永远等待。
如何让套接字正确接收数据?
这个
std::string data = "Hello World!";
net::const_buffer buf(&data, sizeof(data));
是错误的。您希望发送字符串data
内容,而不是其内部字节。&data
为您提供指向字符串实例的基础数据的指针,而不是其内容。如果要创建表示data
内容的缓冲区,可以执行以下操作:
const std::string data = "Hello World!";
net::const_buffer buf = net::buffer(data);
这
net::mutable_buffer buf;
while(buf.size() == 0)
{
socket.receive(buf);
}
为您提供无限循环,因为buf
的初始大小为 0,因此receive
读取 0 字节并返回。然后,在检查条件时,buf 的大小仍为 0,循环继续。
在调用receive
之前,您需要指定缓冲区的大小 - 它指示必须读取多少字节。您正在发送Hello World!
std::string msg;
msg.resize(12); // prepare space for incoming data
net::mutable_buffer buf = net::buffer(msg);
socket.receive(buf);
std::cout << "I got: " << msg << std::endl;
相关文章:
- 在 QNX 中,如何管理服务器和客户端之间的 IPC 连接?
- 如何使用gRPC在客户端和服务器之间双向发送和接收流元数据
- 如何使用标准库网络 TS 在服务器和客户端之间发送消息C++
- TCP在客户端服务器之间传输时,数据是否有任何限制
- 服务器和客户端之间的同步
- 我无法在用C++编写的服务器和用 C# 编写的客户端之间建立 UDP 连接
- 如何将数据序列化为C++ zmq 客户端和 Python zmq 服务器之间的通信
- 如何使用 zeromq(zmq) 在服务器和客户端之间发送整数值
- 通过TCP在客户端/ C#服务器之间传输文件C++
- 无法使用<double> QTcpSocket 和 QDataStream 在服务器和客户端之间传输 QVector
- tensorflow C API中的客户端和会话之间的差异
- 为什么文件没有通过 tcp 在 C# 中的客户端和 C 中的服务器之间正确传输(保存?)
- 无法在 matlab 中的 UDP 客户端和 Linux 中的服务器之间发送数据
- 在 Winsock ws 客户端和 Websocket 服务器之间建立握手
- 在客户端和服务器之间序列化复杂的C++结构
- 客户端JavaScript和C 套接字服务器之间的通信
- 如何在服务器和客户端之间进行异步通信
- C++:服务器和客户端之间的经典通信练习
- 语言之间的服务器客户端首选项
- 本地数据库的密码解决方案,在客户端和服务器之间传递