Boost::asio TCP服务器——从客户端读取消息
Boost::asio TCP server -- reading a message from the client
我正在尝试使用boost::asio创建我的第一个TCP服务器。服务器将监听客户端,如果它收到消息"MESSAGE_SEND_A:",它应该将以下消息发送回客户端:"A|A|A|A|A|A"。如果它接收到消息"MESSAGE_SEND_B:",那么它应该相应地向客户端发送另一条消息:" B|B|B|B|B|B"。
现在,我一直在学习Boost TCP服务器教程,它或多或少是清楚的:
编辑:基于注释重写代码 #include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
// Start reading messages from the server
start_read();
if (messageFromClient_ == "MESSAGEA:")
{
messageToClient_ = "A|A|A|A|A|A|A|A|A|A";
}
else if (messageFromClient_ == "MESSAGEB:")
{
messageToClient_ = "B|B|B|B|B|B|B|B|B|B";
}
boost::asio::async_write(socket_, boost::asio::buffer(messageToClient_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
tcp_connection(boost::asio::io_service& io_service) : socket_(io_service)
{
}
// Reading messages from the server
void start_read()
{
// Start an asynchronous operation to read a newline-delimited message.
// When read, handle_read should kick in
boost::asio::async_read(socket_, input_buffer_,
boost::asio::transfer_at_least(1),
boost::bind(&tcp_connection::handle_read, this,
boost::asio::placeholders::error));
}
// When stream is received, handle the message from the client
void handle_read(const boost::system::error_code& ec)
{
//if (stopped_)
// return;
// Making the message nil every time the function starts. Do I need it???????
messageFromClient_ = "";
if (!ec)
{
// Extract the newline-delimited message from the buffer.
std::string line;
std::istream is(&input_buffer_);
std::getline(is, line);
// Empty messages are heartbeats and so ignored.
if (!line.empty())
{
messageFromClient_ = line;
std::cout << "Received: " << line << "n";
}
start_read();
}
else
{
std::cout << "Error on receive: " << ec.message() << "n";
}
}
void handle_write(const boost::system::error_code& /*error*/,
size_t /*bytes_transferred*/)
{
}
tcp::socket socket_;
std::string messageToClient_;
boost::asio::streambuf input_buffer_;
std::string messageFromClient_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 7767))
{
start_accept();
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void handle_user_read(const boost::system::error_code& err,
std::size_t bytes_transferred)
{
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
start_accept();
}
}
tcp::acceptor acceptor_;
};
int main()
{
try
{
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
代码在Qt上编译得很好,但是给了我:
Error on receive: Operation canceled
每次我试图从我的客户端(iPad)发送消息时。
谢谢!
函数start()应该异步地从套接字读取数据,同时在接收到数据时调用handle_read()函数。请回顾这个例子:http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/timeouts/async_tcp_client.cpp
相关文章:
- 从服务器传输到客户端的消息不会出现
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- Kafka C++客户端需要很长时间才能收到消息
- nng to pynng pub/sub. 客户端未收到任何消息
- C++ UDP 服务器在获取客户端地址配置时发送消息
- 如何使用标准库网络 TS 在服务器和客户端之间发送消息C++
- 客户端发送消息,但服务器在客户端结束程序之前不接收消息
- 节点.js到 c++ 客户端:不接收来自 socket.emit 的消息
- Qt WebSockets,如何向客户端发送消息
- 使 gRPC 客户端处于侦听模式,以侦听来自服务器的消息
- 多线程套接字编程服务器仅从 1 个客户端线程获取消息
- 无法使用C#TCP服务器和C TCP客户端接收消息
- 从客户端到浏览器的CEF中的消息传递序列化
- UDP 客户端不会在 esp32 上广播消息
- 带有 TLS v. 1.2 和 SSL v.3 电子邮件客户端的 Boost.Asio 上下文:ssl3 警报意外消息
- Datagram套接字服务器未从客户端接收消息
- 服务器进程等待客户端输入/消息的最佳方法是什么?
- 为什么我的android客户端无法接收到来自winsock c++服务器的字符串消息
- 套接字编程中的客户端到客户端消息传递
- Qt TCP服务器/多客户端消息读取