当前位置: 首页>>代码示例>>C++>>正文


C++ socket::send_to方法代码示例

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


在下文中一共展示了socket::send_to方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: on_udp_receive

	void on_udp_receive(error_code const& ec, size_t bytes_transferred, udp::endpoint* from, char* buffer, int size)
	{
		if (ec)
		{
			std::printf("%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
			return;
		}

		if (bytes_transferred < 16)
		{
			std::printf("%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
			return;
		}

		if (m_abort)
		{
			return;
		}

		std::printf("%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));

		char* ptr = buffer;
		detail::read_uint64(ptr);
		std::uint32_t const action = detail::read_uint32(ptr);
		std::uint32_t const transaction_id = detail::read_uint32(ptr);

		error_code e;

		switch (action)
		{
			case 0: // connect

				std::printf("%s: UDP connect from %s\n", time_now_string()
					, print_endpoint(*from).c_str());
				ptr = buffer;
				detail::write_uint32(0, ptr); // action = connect
				detail::write_uint32(transaction_id, ptr); // transaction_id
				detail::write_uint64(10, ptr); // connection_id
				m_socket.send_to(boost::asio::buffer(buffer, 16), *from, 0, e);
				if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
					, time_now_string(), e.message().c_str());
				else std::printf("%s: UDP sent response to: %s\n"
					, time_now_string(), print_endpoint(*from).c_str());
				break;

			case 1: // announce

				++m_udp_announces;
				std::printf("%s: UDP announce [%d]\n", time_now_string()
					, int(m_udp_announces));
				ptr = buffer;
				detail::write_uint32(1, ptr); // action = announce
				detail::write_uint32(transaction_id, ptr); // transaction_id
				detail::write_uint32(1800, ptr); // interval
				detail::write_uint32(1, ptr); // incomplete
				detail::write_uint32(1, ptr); // complete
				// 0 peers
				m_socket.send_to(boost::asio::buffer(buffer, 20), *from, 0, e);
				if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
					, time_now_string(), e.message().c_str());
				else std::printf("%s: UDP sent response to: %s\n"
					, time_now_string(), print_endpoint(*from).c_str());
				break;
			case 2:
				// ignore scrapes
				std::printf("%s: UDP scrape (ignored)\n", time_now_string());
				break;
			default:
				std::printf("%s: UDP unknown message: %d\n", time_now_string()
					, action);
				break;
		}

		m_socket.async_receive_from(
			boost::asio::buffer(buffer, size), *from, 0
			, std::bind(&udp_tracker::on_udp_receive, this, _1, _2, from, buffer, size));
	}
开发者ID:Chocobo1,项目名称:libtorrent,代码行数:77,代码来源:udp_tracker.cpp

示例2: handler

void handler(const boost::system::error_code& error, std::size_t /*bytes_transferred*/, udp::socket &sock, udp::endpoint *end)
{
   switch(reinterpret_cast<t_Packet *>(ReceiveBuffer)->type)
   {
      case 0:
      {
         cout<<"I got a default packet"<<endl;
      }
         break;

      case 1: 
      {
         cout<<"Got a ping packet"<<endl;
         cout<<"Its from "<<end->address().to_string()<<endl; 
         
         t_pingPacket pingPacket(*reinterpret_cast<t_pingPacket *>(ReceiveBuffer));
         
         cout<<"The time difference was an amazing "<<(boost::posix_time::microsec_clock::universal_time().time_of_day().total_microseconds() - pingPacket.time)<<endl;
         cout<<"Sending it back...\n\n"<<endl;
         sock.send_to(boost::asio::buffer(ReceiveBuffer),*end);
      }
         break;
      
      case 2:
      {
         cout<<"Someone from "<<end->address().to_string()<<" is trying to join"<<endl;
         
         t_connectPacket connectPacket;
         
         bm::right_map::const_iterator lol = table.right.find(*end);
         if (table.right.end() == lol)
         {
            cout<<"They are not already connected"<<endl;

            connectPacket.id = CurId;
            table.insert(bm::value_type(CurId++,*end));
         }

         else
         {
            cout<<"They connected before"<<endl;

            connectPacket.id=lol->second;
         }

         sock.send_to(boost::asio::buffer(&connectPacket,sizeof(connectPacket)),*end);
         cout<<"We gave them an id of "<<(connectPacket.id)<<endl<<endl;
      }
         break;
      
      case 3:
      {
         cout<<"Someone from "<<end->address().to_string()<<" with id "<<table.right.find(*end)->second<<" wants the world"<<endl;

         t_worldPacket worldPacket(*reinterpret_cast<t_worldPacket *>(ReceiveBuffer));
         worldPacket.numOfObjects = 6;

         cout<<"Telling them the world has five objects"<<endl<<endl;
         sock.send_to(boost::asio::buffer(&worldPacket,sizeof(worldPacket)),*end);
      }
         break;

      case 4:
      {
         cout<<"Someone from "<<end->address().to_string()<<" with id "<<table.right.find(*end)->second<<" wants some objects"<<endl;

         t_objectPacket objectPacket(*reinterpret_cast<t_objectPacket *>(ReceiveBuffer));
         
         for (int i = 0;i<objectPacket.numOfObjects;i++)
         {
            cout<<"He wants the object "<<objectPacket.numbers[i]<<endl;
         }
         
         cout<<"\nMaking new packet"<<endl;

         t_objectPacket *newObjectPacket = reinterpret_cast<t_objectPacket *>(malloc(sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData)));
         memcpy(newObjectPacket,&objectPacket,sizeof(objectPacket));
      
         cout<<"Created new packet of right size(hopefully)"<<endl;
         cout<<"Setting them all to zero, so I can be lazy"<<endl; 

         //memset(newObjectPacket->objectData,0,newObjectPacket->numOfObjects * sizeof(t_objectData));

         cout<<"Setting first to name ";
         strcpy(newObjectPacket->objectData[0].name,"wow");
         cout<<newObjectPacket->objectData[0].name<<endl;

         cout<<"Finially sending the stupid thing over, it has a size of "<<(sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData))<<endl<<endl;
         sock.send_to(boost::asio::buffer(newObjectPacket,sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData)),*end);


      }

      default:
         cout<<"Who knows what packet I got!!"<<endl;
   }  
   
   sock.async_receive_from(boost::asio::buffer(ReceiveBuffer),*end,boost::bind(handler,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred,
//.........这里部分代码省略.........
开发者ID:Lalaland,项目名称:Bullet,代码行数:101,代码来源:main.cpp


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