boost::asio::read防止boost:asio::write向Java Socket发送数据
boost::asio::read prevents boost:asio::write from sending data to Java Socket
我正试图在c++应用程序和Java应用程序之间来回发送数据。c++程序是客户端,java程序是服务器。从客户端发送请求到服务器可以工作,但反过来不行。
当我在boost::asio::write
之后使用boost::asio::read
等待服务器的响应时,服务器没有收到之前通过写发送的数据,客户端在读取时阻塞)。如果取消注释boost::asio::read
命令发送工作
下面是c++客户端类的重要部分:
连接建立:
_socket = new tcp::socket(_io_service);
tcp::no_delay option(true);
tcp::resolver resolver(_io_service);
boost::asio::connect(*_socket, resolver.resolve({_host, _port}));
_socket->set_option(option);
先写后读到套接字:
// Send length of the query
uint32_t length = message.size();
boost::asio::write(*_socket, boost::asio::buffer(&length, 4));
// Now send the message
const char *messageBuffer = message.c_str();
size_t messageBufferLength = std::strlen(messageBuffer);
boost::asio::write(*_socket, boost::asio::buffer(messageBuffer, messageBufferLength));
// Receive length of answer
uint32_t lengthAnswer = 0;
boost::asio::read(*_socket, boost::asio::buffer(&lengthAnswer, 4));
在Java服务器端:
连接通过套接字建立(来自ServerSocket.accept())。然后打开一个DataInputStream和一个DataOutputStream:
this.dis = new DataInputStream(socket.getInputStream());
this.dos = new DataOutputStream(socket.getOutputStream());
所有的读数都是基于这种方法,其余的只是转换等:
private byte[] readBytes(int n) throws IOException {
byte[] data = new byte[n];
dis.readFully(data);
return data;
}
我做错了什么吗?如果没有boost::read(),它也能工作,这真的很奇怪。
看起来代码本身工作得很好。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 从 Boost ASIO 获取 epoll 描述符 io_service对象
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 执行时使用 boost::asio::d eadline_timer 时出错
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- boost::asio data owning `ConstBufferSequence`
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 如何将boost::asio::d eadline_timer 与Qt一起使用?
- 将更高的优先级设置为 boost::asio 线程处理进程
- Async_read_until限制读取的字节大小(Boost::asio)
- 程序崩溃使用boost::asio
- boost::asio 无法捕获 SIGINT
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 如何在 boost::asio 中将打包的结构作为消息传递?(无序列化)
- 如何使用C++和Boost Asio从HTTP发布请求中获取键值
- std::boost::asio::p ost / dispatch 使用哪个io_context?