惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 星星之火

Writing own regular expression parser How Regexes Work Content Based Routing Web Services in .NET and J2EE Poly-Engine Crypt String 绝不因寂寞而爱上别人 比尔·盖茨给青年一代的11点忠告 在.Net环境下用C#操纵活动目录 - 星星之火 - 博客园 谁能简单说一下活动目录是什么东西? 广播,多播(二)(Broadcasting, Multicasting) 两毛钱爽一把 可以建立一个Udp Server,接收发往本机所有端口的数据包吗? C#异步网络编程 应该由国家建立非法网站数据库 use Helper Classes to simplify you network programming a udp echo client a udp echo server xml digital signature when udp goes bad and how to solve it(C#) when tcp goes bad, and how to solve it
广播,多播(一)(Broadcasting, Multicasting)
星星之火 · 2004-06-23 · via 博客园 - 星星之火

前言

在网络编程中,通过广播和多播可以实现发送端发送一个数据包,有多个接收端接收的情况。

广播

由于Tcp是有连接的,所以不能用来发送广播消息。发送广播消息,必须用到UdpUdp可以不用建立连接而发送消息。广播消息的目的IP地址是一种特殊IP地址,称为广播地址。广播地址由IP地址网络前缀加上全1主机后缀组成,如:192.168.1.255192.169.1.0这个网络的广播地址;130.168.255.255130.168.0.0这个网络的广播地址。向全部为1IP地址(255.255.255.255)发送消息的话,那么理论上全世界所有的联网的计算机都能收得到了。但实际上不是这样的,一般路由器上设置抛弃这样的包,只在本地网内广播,所以效果和向本地网的广播地址发送消息是一样的。

C#中发送广播消息的过程如下,注意要调用SetSockOption函数,不然要抛出异常:

   Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                ProtocolType.Udp);
   IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);//255.255.255.255
   IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
   string hostname = Dns.GetHostName();
   byte[] data = Encoding.ASCII.GetBytes(hostname);
   sock.SetSocketOption(SocketOptionLevel.Socket,
              SocketOptionName.Broadcast, 1);
   sock.SendTo(data, iep1);
   sock.SendTo(data, iep2);
   sock.Close();

C#中接收广播消息的过程如下,没有什么特别的:

   Socket sock = new Socket(AddressFamily.InterNetwork,
           SocketType.Dgram, ProtocolType.Udp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
   sock.Bind(iep);
   EndPoint ep = (EndPoint)iep;
   Console.WriteLine("Ready to receive");
   byte[] data = new byte[1024];
   int recv = sock.ReceiveFrom(data, ref ep);
   string stringData = Encoding.ASCII.GetString(data, 0, recv);
   Console.WriteLine("received: {0} from: {1}",
              stringData, ep.ToString());
   data = new byte[1024];
   recv = sock.ReceiveFrom(data, ref ep);
   stringData = Encoding.ASCII.GetString(data, 0, recv);
   Console.WriteLine("received: {0} from: {1}",
              stringData, ep.ToString());
   sock.Close();
Attentions:
1 广播可以由客户程序来通知子网内的服务程序,自己的位置。

2 发送广播消息时指定的端口也是有作用的,接收端的Udp Socket如果Bind到此接口的话,就能够接收到消息。(如果不Bind的话,就能接收所有端口的消息??)

3 接收端接到的包中显示的远端IP地址是发送端的地址。就是说广播地址不会显示到一个包的源IP地址位置(LoopBack地址不会显示到包的目的IP地址位置)

4 可以用线程不停地发送和接收广播消息,确认两端的位置和证明自己的存在

上面的程序来自(C# Network Programming