我正在通过boost asio教程尝试学习如何使用他们的网络库。我实现了示例日间客户端/服务器here(这只是服务器)。在运行客户端和服务器时,我得到无法分配大小为mach_vm_map(size=7597121102135848960)
的区域。
我不知道为什么会这样。
服务器:
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string() {
using namespace std;
time_t now = time(NULL);
return ctime(&now);
}
int main() {
try {
boost::asio::io_service io;
tcp::acceptor acceptor(io);
tcp::endpoint endpoint(tcp::v4(), 13);
acceptor.open(endpoint.protocol());
acceptor.set_option(tcp::acceptor::reuse_address(true));
acceptor.bind(endpoint); // throwing error here
acceptor.listen();
for(;;) {
tcp::socket socket(io);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::system::error_code ignore_error;
boost::asio::write(socket, boost::asio::buffer(message), ignore_error);
}
}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
客户端:
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char ** argv) {
try {
if(argc != 2) {
std::cerr << "Usage: client <host>" << std::endl;
return 1;
}
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator); // error here
for(;;) {
boost::array<char, 128> buf;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if(error == boost::asio::error::eof)
break;
else if(error)
throw boost::system::system_error(error);
std::cout.write(buf.data(), len);
}
}
catch(std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
只有不同尺寸的错误基本相同:
错误:
sserver(14988,0x7fffdc3283c0) malloc: *** mach_vm_map(size=7597121102135848960) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
sserver(14988,0x7fffdc3283c0) malloc:
*** error for object 0x7373696d72655022: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[1] 14988 abort ./sserver
我知道怎么解决这个问题我会非常感激。我应该注意到我正在使用g ++ 6.1.0编译并提升1.60.02。