client端:
//定义一个socket,连接本机
asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("127.0.0.1", "13");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
io_service.run();
socket.close();
//连接
std::cout << "prepare to connect" << std::endl;
socket.async_connect(*endpoint_iterator++, test);
std::cout << "connect end" << std::endl;
std::string s = socket.remote_endpoint().address().to_string();
std::cout << s << std::endl;
char buf[128];
Sleep(2000);
for (;;)
{
//得到数据
Sleep(2000);
memset(buf, 0, 128);
size_t len = socket.read_some(asio::buffer(buf));
std::cout << "read finished" << std::endl;
f<< buf << std::endl;
}
server的代码差不多,就是read_some 改成 write_some....
想尝试 异步的,async_read, async_write....然后就出问题了。。。sigh....
不知道是什么原因
No comments:
Post a Comment