本文整理汇总了C++中boost::asio::ip::udp::socket::bind方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::bind方法的具体用法?C++ socket::bind怎么用?C++ socket::bind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::ip::udp::socket
的用法示例。
在下文中一共展示了socket::bind方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createSingleSocket
bool STUNClient::createSingleSocket(
const std::string& stunServer,
boost::asio::ip::udp::socket& socket,
const OSS::IPAddress& lAddr,
OSS::IPAddress& externalAddress)
{
OSS::mutex_critic_sec_lock globalLock(_csGlobal);
_test1Responded = false;
_test2Responded = false;
_test3Responded = false;
_test10Responded = false;
_test1ChangedAddr = OSS::IPAddress();
_test1MappedAddr = OSS::IPAddress();
_test10MappedAddr = OSS::IPAddress();
_sendCount = 0;
OSS::IPAddress localAddress = lAddr;
OSS::IPAddress targetAddress = OSS::IPAddress::fromV4IPPort(stunServer.c_str());
if (!targetAddress.isValid() || !localAddress.isValid())
return false;
if (!targetAddress.getPort())
targetAddress.setPort(STUN_PORT);
if (!socket.is_open())
{
socket.open(boost::asio::ip::udp::v4());
boost::asio::ip::udp::endpoint ep;
ep = boost::asio::ip::udp::endpoint(localAddress.address(), localAddress.getPort());
boost::system::error_code ec;
socket.bind(ep, ec);
if (ec)
return false;
}
while (_sendCount < 15)
{
_sendCount++;
if (!_test1Responded)
sendTestRequest(socket, targetAddress, 1);
else
break;
}
if (_test1Responded)
{
externalAddress = _test1MappedAddr;
return externalAddress.isValid();
}
return false;
}
示例2: interfaces
interfaces() {
memset(data,0,MAX_LINKS*sizeof(struct info));
memset(buf,0,MAX_BUF);
lsock.open(boost::asio::ip::udp::v4());
lsock.bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),10000));
lsock.async_receive(
boost::asio::buffer(buf,MAX_BUF),
boost::bind(&interfaces::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
boost::thread t = boost::thread (boost::bind(&boost::asio::io_service::run, &iol));
}
示例3: thread
SC_UdpInPort(struct World * world, int inPortNum):
mWorld(world), mPortNum(inPortNum), udpSocket(ioService)
{
using namespace boost::asio;
BOOST_AUTO(protocol, ip::udp::v4());
udpSocket.open(protocol);
udpSocket.bind(ip::udp::endpoint(protocol, inPortNum));
boost::asio::socket_base::send_buffer_size option(65536);
udpSocket.set_option(option);
#ifdef USE_RENDEZVOUS
if (world->mRendezvous) {
thread thread( boost::bind( PublishPortToRendezvous, kSCRendezvous_UDP, sc_htons(mPortNum) ) );
mRendezvousThread = std::move(thread);
}
#endif
startReceiveUDP();
}
示例4: receiver
receiver(boost::asio::io_service& io_service,
const boost::asio::ip::address& listen_address,
const boost::asio::ip::address& multicast_address)
: socket_(io_service)
{
// Create the socket so that multiple may be bound to the same address.
boost::asio::ip::udp::endpoint listen_endpoint(
listen_address, multicast_port);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);
// Join the multicast group.
socket_.set_option(
boost::asio::ip::multicast::join_group(multicast_address));
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&receiver::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
示例5: 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;
}