Linux系统下UDP发送和接收广播消息小样例
- // 发送端
- #include <iostream>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string.h>
- using namespace std;
- int main()
- {
- setvbuf(stdout, NULL, _IONBF, 0);
- fflush(stdout);
- int sock = -1;
- if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- {
- cout<<"socket error"<<endl;
- return false;
- }
- const int opt = 1;
- //设置该套接字为广播类型,
- int nb = 0;
- nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
- if(nb == -1)
- {
- cout<<"set socket error..."<<endl;
- return false;
- }
- struct sockaddr_in addrto;
- bzero(&addrto, sizeof(struct sockaddr_in));
- addrto.sin_family=AF_INET;
- addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST);
- addrto.sin_port=htons(6000);
- int nlen=sizeof(addrto);
- while(1)
- {
- sleep(1);
- //从广播地址发送消息
- char smsg[] = {"abcdef"};
- int ret=sendto(sock, smsg, strlen(smsg), 0, (sockaddr*)&addrto, nlen);
- if(ret<0)
- {
- cout<<"send error...."<<ret<<endl;
- }
- else
- {
- printf("ok ");
- }
- }
- return 0;
- }
- // 接收端
- #include <iostream>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string.h>
- using namespace std;
- int main()
- {
- setvbuf(stdout, NULL, _IONBF, 0);
- fflush(stdout);
- // 绑定地址
- struct sockaddr_in addrto;
- bzero(&addrto, sizeof(struct sockaddr_in));
- addrto.sin_family = AF_INET;
- addrto.sin_addr.s_addr = htonl(INADDR_ANY);
- addrto.sin_port = htons(6000);
- // 广播地址
- struct sockaddr_in from;
- bzero(&from, sizeof(struct sockaddr_in));
- from.sin_family = AF_INET;
- from.sin_addr.s_addr = htonl(INADDR_ANY);
- from.sin_port = htons(6000);
- int sock = -1;
- if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- {
- cout<<"socket error"<<endl;
- return false;
- }
- const int opt = 1;
- //设置该套接字为广播类型,
- int nb = 0;
- nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
- if(nb == -1)
- {
- cout<<"set socket error..."<<endl;
- return false;
- }
- if(bind(sock,(struct sockaddr *)&(addrto), sizeof(struct sockaddr_in)) == -1)
- {
- cout<<"bind error..."<<endl;
- return false;
- }
- int len = sizeof(sockaddr_in);
- char smsg[100] = {0};
- while(1)
- {
- //从广播地址接受消息
- int ret=recvfrom(sock, smsg, 100, 0, (struct sockaddr*)&from,(socklen_t*)&len);
- if(ret<=0)
- {
- cout<<"read error...."<<sock<<endl;
- }
- else
- {
- printf("%s\t", smsg);
- }
- sleep(1);
- }
- return 0;
- }
Linux系统下UDP发送和接收广播消息小样例的更多相关文章
- Linux系统下UDP发送和接收广播消息小例子
// 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...
- linux系统下邮件的发送
在linux系统下发送邮件一般都要要求本地的机器必须安装和启动Sendmail服务,配置非常麻烦,而且会带来不必要的资源占用. 其实我还可以安装mailx软件,通过修改配置文件可以使用外部SMTP服务 ...
- Linux系统下DHCP服务安装部署和使用详解
一.概述 DHCP :动态主机设置协议(英语:Dynamic Host Configuration Protocol,DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途:用于内部网或 ...
- 在Linux系统下mail命令的用法
在Linux系统下mail命令的测试 1. 最简单的一个例子: mail -s test admin@aispider.com 这条命令的结果是发一封标题为test的空信给后面的邮箱,如果你有mta并 ...
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
http://www.ibm.com/developerworks/cn/linux/l-ipmi/ 1.简介 IPMI(Intelligent Platform Management Interfa ...
- Linux系统下安装Redis和Redis集群配置
Linux系统下安装Redis和Redis集群配置 一. 下载.安装.配置环境: 1.1.>官网下载地址: https://redis.io/download (本人下载的是3.2.8版本:re ...
- Linux系统下如何优雅地关闭Java进程?
资料出处: http://www.sohu.com/a/329564560_700886 https://www.cnblogs.com/nuccch/p/10903162.html 前言 Linux ...
- Linux 系统下安装 rz/sz 命令及使用说明
Linux 系统下安装 rz/sz 命令及使用说明 rz/sz命令,实现将本地的文件上传到服务器或者从服务器上下载文件到本地,但是很多Linux系统初始并没有这两个命令,以下为安装和使用的具体步骤: ...
- 深入理解linux系统下proc文件系统内容
深入理解linux系统下proc文件系统内容 内容摘要:Linux系统上的/proc目录是一种文件系统,即proc文件系统. Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它 ...
随机推荐
- PHP程序的一次重构记录
项目和新需求: 我们有一个PHP写的webmail系统,有一个mail_list.php用于展现用户的邮件列表这个页面支持folderId参数(因为邮件是存在不同的文件夹下的)由于邮件太多所以支持翻页 ...
- 理解和熟练运用js中的call及apply
call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...
- uva 11437 - Triangle Fun
计算几何: 直线交点: #include<cstdio> using namespace std; struct node { double x,y; node(,):x(x),y(y){ ...
- uva 10881 - Piotr's Ants
这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...
- PYTHON小CASE
import os import time source = ['C:\\py\\', 'C:\\work\\'] target_dir = 'C:\\backup' today = target_d ...
- html表格cell合并插件
数据展示时需要合并部分数据自己写了一个简单插件 合并前: 合并后: 调用示例: var trs = $('table#dataList tbody tr').not('#demo').get(); v ...
- UNDO表空间损坏,爆满,ORA-600[4194]/[4193]错误解决
模拟手工删除UNDO表空间 在ORADATA 中把UNDOTBS01.DBF 删除 模拟启库 SQL> STARUP; * 第 1 行出现错误: ORA-01157: 无法标识/锁定数据文件 2 ...
- git图示所有分支的历史
1.第一种方法 git gui 菜单栏上 repository-->visual all branch history 或者直接使用命令gitk --all 2.在git bash中,使用命令查 ...
- 论游戏中Buff的实现 [转]
论游戏中Buff的实现 分类: C/C++ 游戏开发2012-09-13 14:30 574人阅读 评论(6) 收藏 举报 c++游戏开发 源地址:http://blog.codingnow.com/ ...
- B. Berland Bingo
Lately, a national version of a bingo game has become very popular in Berland. There are n players p ...