• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ udp::socket类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中boost::asio::ip::udp::socket的典型用法代码示例。如果您正苦于以下问题:C++ socket类的具体用法?C++ socket怎么用?C++ socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了socket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:arnaudcoquelet,项目名称:oss_core,代码行数:59,代码来源:STUNClient.cpp


示例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;
}
开发者ID:arnaudcoquelet,项目名称:oss_core,代码行数:54,代码来源:STUNClient.cpp


示例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");
}
开发者ID:sfff,项目名称:example,代码行数:13,代码来源:example.cpp


示例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));
 }
开发者ID:promovicz,项目名称:garmin-radar,代码行数:7,代码来源:garmin.cpp


示例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();
    }
}
开发者ID:huangyt,项目名称:MyProjects,代码行数:29,代码来源:Detection.cpp


示例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)));
 }
开发者ID:promovicz,项目名称:garmin-radar,代码行数:7,代码来源:garmin.cpp


示例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)));
 }
开发者ID:promovicz,项目名称:garmin-radar,代码行数:7,代码来源:garmin.cpp


示例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
         )
     );
 }
开发者ID:Szperak,项目名称:sAudio-receiver,代码行数:8,代码来源:client.cpp


示例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();
    }
开发者ID:Szperak,项目名称:sAudio-receiver,代码行数:17,代码来源:client.cpp


示例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;      
}
开发者ID:CCJY,项目名称:coliru,代码行数:9,代码来源:main.cpp


示例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();
}
开发者ID:Ablu,项目名称:freeorion,代码行数:9,代码来源:ServerNetworking.cpp


示例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());
    }
开发者ID:ijon,项目名称:blackhole,代码行数:11,代码来源:udp.cpp


示例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
         )
     );
 }
开发者ID:Szperak,项目名称:sAudio-receiver,代码行数:11,代码来源:client.cpp


示例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();
	}
开发者ID:dailypips,项目名称:supercollider,代码行数:21,代码来源:SC_ComPort.cpp


示例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));
  }
开发者ID:HongSeokHwan,项目名称:legacy,代码行数:22,代码来源:receiver.cpp


示例16: receive

void Server::receive() {
    Peer peer;
    boost::system::error_code error;
    auto bytes_transferred =
      socket_.receive_from(boost::asio::buffer(recv_buffer_), peer, 0, error);
    std::cout << "Server received " << bytes_transferred << " bytes"
                 " from " << peer
              << " with " << error.message() << std::endl;
    if (!error) {
      std::cout << "  >> ";
      std::cout.write(recv_buffer_.data(), bytes_transferred);
      std::cout << std::endl;
    }
    receive();
}
开发者ID:CCJY,项目名称:coliru,代码行数:15,代码来源:main.cpp


示例17: handle_receive_from

  void handle_receive_from(
      const boost::system::error_code& error,
      size_t bytes_recvd)
  {
    if (!error)
    {
      std::cout.write(data_, bytes_recvd);
      std::cout << std::endl;

      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));
    }
  }
开发者ID:HongSeokHwan,项目名称:legacy,代码行数:16,代码来源:receiver.cpp


示例18: SendPacket

 bool SendPacket(const P &packet) {
   boost::system::error_code ec;
   return socket.send_to(boost::asio::buffer(&packet, sizeof(packet)),
                         endpoint, 0, ec) == sizeof(packet) && !ec;
 }
开发者ID:groundhogxc,项目名称:XCSoar,代码行数:5,代码来源:Client.hpp


示例19:

inline bool
UdpChannel::isListening() const
{
  return m_socket.is_open();
}
开发者ID:MobinRanjbar,项目名称:NFD,代码行数:5,代码来源:udp-channel.hpp


示例20: SendPacket

 void SendPacket(const P &packet) {
   const ScopeLock protect(mutex);
   socket.send_to(boost::asio::buffer(&packet, sizeof(packet)),
                  endpoint, 0);
 }
开发者ID:ahsparrow,项目名称:xcsoar,代码行数:5,代码来源:Client.hpp



注:本文中的boost::asio::ip::udp::socket类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ compute::command_queue类代码示例发布时间:2022-05-31
下一篇:
C++ udp::endpoint类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap