求助万能的8u们
c++吧
全部回复
仅看楼主
level 7
boost库写的服务器和客户端的异步通信代码,假设服务器发送一条''hello asio''的字符串,客户端接受并打印出来,如果接受缓冲区大小指定为3和4,最后打印的结果总是会多出一两个字符。。不知道怎么回事,只在windows平台上测过,linux上没测过,因为boost的库太大了,没装到虚拟机上面。
2022年11月21日 14点11分 1
level 7
服务器代码
2022年11月21日 14点11分 2
level 7
客户端代码
2022年11月21日 14点11分 3
level 7
错误示例
2022年11月21日 14点11分 4
level 8
是不是没设置结束符。。。 片段看不出啥
2022年11月21日 15点11分 5
没有结束符,会一直不停的打印,这个结束标记是用错误码来反应的(类似于linux的errno)
2022年11月21日 15点11分
是不是客户端代码被吞了,看不见了?发了两遍好像都不管用
2022年11月21日 23点11分
level 7
读取完数据后,如果只用m_buf.resize(5,0)并不管用,还要在前面加一句m_buf.clear(),很难理解[怒],直接打印&m_buf[0]是一堆乱码
2022年11月21日 16点11分 7
resize貌似不会改变原来已经存在的数据
2022年11月22日 06点11分
@ThegiantMaster 多谢提醒,恍然大悟[真棒]
2022年11月22日 06点11分
level 5
客户端代码呢
2022年11月21日 16点11分 8
三楼,是不是被吞了?
2022年11月21日 16点11分
@花枫不是殇 三楼没了,再发吧
2022年11月21日 16点11分
@rqb500 补了,似乎又被吞了
2022年11月21日 22点11分
level 7
不知道客户端代码是不是被吞了,补一下
2022年11月21日 16点11分 9
level 7
2022年11月21日 22点11分 10
level 7
2022年11月21日 22点11分 11
level 7
2022年11月21日 22点11分 12
level 7
客户端代码10~12楼,别在被吞了[怒]
2022年11月21日 22点11分 13
level 7
客户端代码:
//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING
#include<iostream>
#include<boost/asio.hpp>
#include<boost/asio/ip/tcp.hpp>
#include<syncstream>
#include<chrono>
#include<string>
#include<iomanip>
using namespace std;
using namespace boost::asio;
using namespace std::chrono;
//if use g++ complie in windows,write like this:
//g++ -o server 网页链接 -IC:\boost_1_80_0 -LC:\boost_1_80_0\stage\lib -lws2_32 -lwsock32
class client
{
typedef client this_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::address address_type;
typedef ip::tcp::socket socket_type;
typedef shared_ptr<socket_type> sock_ptr;
typedef vector<char> buffer_type;
static const size_t MAXSIZE = 5;
private:
io_service m_io;
buffer_type m_buf;
endpoint_type m_ep;
public:
client()
:m_buf(MAXSIZE, 0), m_ep(address_type::from_string("127.0.0.1"), 6688)
{
start();
}
void run()
{
m_io.run();
}
private:
void start()
{
sock_ptr sock(new socket_type(m_io));
boost::system::error_code ec;
sock->async_connect(m_ep,
[this, sock](const boost::system::error_code& ec)
{
if (ec)
return;
cout << "receive from " << sock->remote_endpoint().address() << ": ";
sock->async_read_some(buffer(m_buf),
[this, sock](const boost::system::error_code& ec, std::size_t)
{
read_handler(ec, sock);
});
});
}
void read_handler(const boost::system::error_code& ec, sock_ptr sock)
{
if (ec)
return;
for (auto data : m_buf)cout << data;
m_buf.resize(MAXSIZE, 0);
sock->async_read_some(buffer(m_buf),
[this, sock](const boost::system::error_code& ec, std::size_t)
{
read_handler(ec, sock);
});
}
};
int main()
{
try
{
cout << "client start." << endl;
client cl;
cl.run();
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
2022年11月21日 23点11分 14
level 7
服务器代码:
//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING
#include<iostream>
#include<boost/asio.hpp>
#include<boost/asio/ip/tcp.hpp>
#include<syncstream>
#include<chrono>
#include<string>
using namespace std;
using namespace boost::asio;
using namespace std::chrono;
//if use g++ complie in windows,write like this:
//g++ -o server 网页链接 -IC:\boost_1_80_0 -LC:\boost_1_80_0\stage\lib -lws2_32 -lwsock32
class server
{
typedef server this_type;
typedef ip::tcp::acceptor acceptor_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::tcp::socket socket_type;
typedef shared_ptr<socket_type> sock_ptr;
private:
io_service m_io;
acceptor_type m_acceptor;
public:
server() :m_acceptor(m_io, endpoint_type(ip::tcp::v4(), 6688)) { accept(); }
void run() { m_io.run(); }
void accept()
{
sock_ptr sock(new socket_type(m_io));
boost::system::error_code ec;
m_acceptor.async_accept(*sock, [this, sock](const boost::system::error_code& ec) {
if (ec)
return;
cout << "client: ";
cout << sock->remote_endpoint().address() << endl;
char msg[] = "hello asio";
sock->async_write_some(buffer(msg,strlen(msg)),
[](const boost::system::error_code&, std::size_t)
{cout << "send msg complete." << endl; }
);
accept();
});
}
};
int main()
{
try
{
cout << "server start." << endl;
server srv;
srv.run();
}
catch (std::exception& e)
{
cout << e.what() << endl;
}
}
2022年11月21日 23点11分 15
level 7
整段代码发出来,直接被吞,我这边直接不显示[怒]
2022年11月21日 23点11分 16
1 2 尾页