• asio 广播代码示例


    代码网络收集 修改了一个编译的小问题

    客户端

    // Client.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    
    #include <boost/asio.hpp>
    
    int main()
    {
    	namespace ip = boost::asio::ip;
    	boost::asio::io_service io_service;
    
    	// Client binds to any address on port 8888 (the same port on which
    	// broadcast data is sent from server).
    	ip::udp::socket socket(io_service,
    		ip::udp::endpoint(ip::udp::v4(), 8888));
    
    	ip::udp::endpoint sender_endpoint;
    
    	// Receive data.
    	//boost::array<char, 4> buffer;
    	char buf[500] = { 0 };
    	std::size_t bytes_transferred =
    		socket.receive_from(boost::asio::buffer(buf), sender_endpoint);
    
    	std::cout << "got " << bytes_transferred << " bytes." <<  buf <<std::endl;
    }
    

      服务端

    // Server.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <boost/asio.hpp>
    
    int main()
    {
    	namespace ip = boost::asio::ip;
    	boost::asio::io_service io_service;
    
    	// Server binds to any address and any port.
    	ip::udp::socket socket(io_service,
    		ip::udp::endpoint(ip::udp::v4(), 0));
    	socket.set_option(boost::asio::socket_base::broadcast(true));
    
    	// Broadcast will go to port 8888.
    	ip::udp::endpoint broadcast_endpoint(ip::address_v4::broadcast(), 8888);
    
    	// Broadcast data.
    	//boost::array<char, 4> buffer;
    	char* buf = "测试代码";
    	socket.send_to(boost::asio::buffer(buf,strlen(buf)+1), broadcast_endpoint);
    }
    

      

  • 相关阅读:
    我的插件框架·前传
    在OpenSUSE中听歌
    ASP.NET MVC 3.0 源码阅读手记(1)
    Mono on Linux 开发与实践札记(1)
    探讨对Web控件的异常处理
    进销存管理中负库存产生的原因以及对应措施
    看了一篇不错的文章 使用 UTF8 对 XML 文档进行编码
    进销存管理中对红冲处理的误区
    Ajax学习笔记(2) 一定要用XML吗?
    打造自己的Html文本编辑控件
  • 原文地址:https://www.cnblogs.com/itdef/p/5242332.html
Copyright © 2020-2023  润新知