asio 广播代码示例
2024-09-04 10:28:39
代码网络收集 修改了一个编译的小问题
客户端
// 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);
}
最新文章
- Daily Scrum Meeting ——EighthDay
- XAF点滴:很具体很用实用---处理三个小问题
- kaggle数据挖掘竞赛初步--Titanic<;随机森林&;特征重要性>;
- 关于asp.net impersonation的一些谣传
- 20160817_Redis配置操作
- 20个最漂亮的基于WordPress的企业网站
- iOS的I/O操作
- select实现输入模糊匹配与选择双重功能
- 转。管理Gearman
- iOS项目开发中的目录结构
- 去蓝港在线面试Unity3D的笔试题。难吗?知道答案的在评论里写出来分享
- iOS开发那些事儿(四)the dark arts of the Objective-C runtime
- 蓝桥杯java高职组
- CodeForces 660D Number of Parallelograms
- Javascript自动化文档工具JSDuck在Windows下的使用心得
- vscode 中 eslint 相关配置
- dts中memreserve和reserved-memory的区别 (转)
- 【OpenGL】第一个窗口
- .Net(c#)加密解密工具类:
- 配置Spring框架编写XML的提示
热门文章
- Postman的基础使用
- 使用 SignalR与SSE(Sever sent event)向客户端推送提示信息
- TCP/IP协议和IP
- c++builder XE7 C++11 C++0x 新语法
- Activity服务类-7 RepositoryService服务类
- jquery formValidator 表单验证插件, ajax无法传值到后台问题的解决
- hibernate-注解及配置
- 在JSP中,使用get提交方式出现乱码时,为什么要使用new String(s.getBytes(";iso-8859-1";),";utf-8";);?
- Mysql count+if 函数结合使用
- while and for 2