本文整理汇总了C++中boost::asio::ip::udp::socket::close方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::close方法的具体用法?C++ socket::close怎么用?C++ socket::close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::ip::udp::socket
的用法示例。
在下文中一共展示了socket::close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TimeOutHandler
void Detection::TimeOutHandler(int packet_type, const boost::system::error_code& error, boost::asio::ip::udp::socket& socket, int tracker_index)
{
if(!error)//超时
{
std::ofstream fout("text.txt", std::ofstream::ate|std::ofstream::app);
if (packet_type == 0 && !has_tracker_list) // bs
{
fout << "bs回包超时" << std::endl;
fout.close();
m_progress_pos_ += 3;
}
else if (packet_type == 1) // tracker
{
std::string result = "";
result.append("tracker(");
result.append(m_tracker_infos[tracker_index].ip);
result.append(":");
boost::uint16_t port = m_tracker_infos[tracker_index].port;
char tmp[10];
itoa(m_tracker_infos[tracker_index].port, tmp, 10);
result.append(tmp);
result.append( ")回包超时");
result_trackers.push_back(result);
//fout.close();
m_progress_pos_ += 1;
}
socket.close();
}
}
示例2: TryBindSocket
std::string AutohostInterface::TryBindSocket(
boost::asio::ip::udp::socket& socket,
const std::string& remoteIP, int remotePort,
const std::string& localIP, int localPort)
{
std::string errorMsg = "";
ip::address localAddr;
ip::address remoteAddr;
boost::system::error_code err;
try {
socket.open(ip::udp::v6(), err); // test IP v6 support
const bool supportsIPv6 = !err;
remoteAddr = netcode::WrapIP(remoteIP, &err);
if (err) {
throw std::runtime_error("Failed to parse address " + remoteIP + ": " + err.message());
}
if (!supportsIPv6 && remoteAddr.is_v6()) {
throw std::runtime_error("IP v6 not supported, can not use address " + remoteAddr.to_string());
}
if (localIP.empty()) {
// use the "any" address as local "from"
if (remoteAddr.is_v6()) {
localAddr = ip::address_v6::any();
} else {
if (supportsIPv6) {
socket.close();
}
socket.open(ip::udp::v4());
localAddr = ip::address_v4::any();
}
} else {
localAddr = netcode::WrapIP(localIP, &err);
if (err) {
throw std::runtime_error("Failed to parse local IP " + localIP + ": " + err.message());
}
if (localAddr.is_v6() != remoteAddr.is_v6()) {
throw std::runtime_error("Local IP " + localAddr.to_string() + " and remote IP " + remoteAddr.to_string() + " are IP v4/v6 mixed");
}
}
socket.bind(ip::udp::endpoint(localAddr, localPort));
boost::asio::socket_base::non_blocking_io command(true);
socket.io_control(command);
// A similar, slighly less verbose message is already in GameServer
//LogObject() << "[AutohostInterface] Connecting (UDP) to IP "
// << (remoteAddr.is_v6() ? "(v6)" : "(v4)") << " " << remoteAddr
// << " Port " << remotePort;
socket.connect(ip::udp::endpoint(remoteAddr, remotePort));
} catch (std::runtime_error& e) { // includes also boost::system::system_error, as it inherits from runtime_error
socket.close();
errorMsg = e.what();
if (errorMsg.empty()) {
errorMsg = "Unknown problem";
}
}
return errorMsg;
}