本文整理汇总了C++中boost::asio::ip::udp::socket类的典型用法代码示例。如果您正苦于以下问题:C++ socket类的具体用法?C++ socket怎么用?C++ socket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了socket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendTestRequest
void STUNClient::sendTestRequest(
boost::asio::ip::udp::socket& sock,
OSS::IPAddress& dest,
int testNum)
{
//std::cout << "Sending TEST " << testNum << " " << sock.local_endpoint().address().to_string()
// << "->" << dest.toIpPortString() << std::endl;
StunAtrString username;
StunAtrString password;
username.sizeValue = 0;
password.sizeValue = 0;
bool changePort=false;
bool changeIP=false;
switch (testNum)
{
case 1:
case 10:
case 11:
break;
case 2:
//changePort=true;
changeIP=true;
break;
case 3:
changePort=true;
break;
case 4:
changeIP=true;
break;
case 5:
break;
default:
OSS_ASSERT(false);
}
StunMessage req;
memset(&req, 0, sizeof(StunMessage));
stunBuildReqSimple(&req, username, changePort , changeIP , testNum);
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
len = stunEncodeMessage(req, buf, len, password, false);
sock.async_send_to(boost::asio::buffer(buf, len),
boost::asio::ip::udp::endpoint(dest.address(), dest.getPort()),
boost::bind(&STUNClient::handleWrite, shared_from_this(),
boost::asio::placeholders::error));
sock.async_receive_from(boost::asio::buffer(_buffer), _senderEndPoint,
boost::bind(&STUNClient::handleReadNatType, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
_semReadEvent.tryWait(10);
}
示例2: 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;
}
示例3: echo
void session::echo(std::vector<uint8_t> const& request)
{
auto reply = request;
socket_.send_to(boost::asio::buffer(request), endpoint_);
std::size_t reply_length = socket_.receive_from(boost::asio::buffer(reply), endpoint_);
if (reply_length != request.size())
throw std::logic_error("Different size of reply and request");
if (reply != request)
throw std::logic_error("Different data value of request and reply");
}
示例4: receive_next
void receive_next() {
m_multicast_socket.async_receive_from
(boost::asio::buffer(m_frame_data, sizeof(m_frame_data)), m_frame_sender,
boost::bind(&garmin_radar::handle_frame, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
示例5: 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();
}
}
示例6: send_control_uchar
void send_control_uchar(unsigned int type, unsigned char data) {
frame_control_uchar frame;
frame.frame_type = type;
frame.len1 = 1;
frame.data = data;
m_control_socket.send(boost::asio::buffer(&frame, sizeof(frame)));
}
示例7: send_control_ushort
void send_control_ushort(unsigned int type, unsigned short data) {
frame_control_ushort frame;
frame.frame_type = type;
frame.len1 = 2;
frame.data = data;
m_control_socket.send(boost::asio::buffer(&frame, sizeof(frame)));
}
示例8: send_request
void send_request(){
socket.async_send_to(boost::asio::buffer(send_arr, 5), receiver_endpoint,
boost::bind(&sAudioReceiver::handle_send, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
示例9: thx
sAudioReceiver(boost::asio::io_service& io_service, sAudioBuffer* audiobuf, char* host, int port) : audiobuf(audiobuf), socket(io_service), ping_interval(boost::posix_time::seconds(1)) {
printf("Creating sAudioReceiver on %s:%d\n", host, port);
socket.open(boost::asio::ip::udp::v4());
receiver_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(host), port);
std::string thx("ihazo");
std::copy(thx.begin(), thx.end(), send_arr.begin());
printf("Created sAudioReceiver\n");
send_request();
start_receive();
printf("Sent request\n");
ping_timer = new boost::asio::deadline_timer(socket.get_io_service(), ping_interval);
start_timer();
}
示例10: send
void Client::send(Peer peer) {
boost::asio::streambuf request;
std::ostream os(&request);
os << "Client";
boost::system::error_code error;
auto bytes_transferred = socket_.send_to(request.data(), peer, 0, error);
std::cout << "Client sent: " << bytes_transferred << " bytes"
" with " << error.message() << std::endl;
}
示例11: HandleReceive
void DiscoveryServer::HandleReceive(const boost::system::error_code& error) {
if (!error &&
std::string(m_recv_buffer.begin(), m_recv_buffer.end()) == DISCOVERY_QUESTION) {
m_socket.send_to(
boost::asio::buffer(DISCOVERY_ANSWER + boost::asio::ip::host_name()),
m_remote_endpoint);
}
Listen();
}
示例12: socket
blocking_t(const std::string& host, std::uint16_t port) :
io_service(),
socket(io_service)
{
boost::asio::ip::udp::resolver resolver(io_service);
boost::asio::ip::udp::resolver::query query(host, boost::lexical_cast<std::string>(port),
boost::asio::ip::udp::resolver::query::flags::numeric_service);
endpoint = *resolver.resolve(query);
socket.open(endpoint.protocol());
}
示例13: start_receive
void start_receive(){
socket.async_receive_from(
boost::asio::buffer(recv_arr), receiver_endpoint,
boost::bind(
&sAudioReceiver::handle_receive,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
示例14: 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();
}
示例15: 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));
}