会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
代码改变世界
Cnblogs
Dashboard
Login
Home
Contact
Gallery
Subscribe
RSS
等待
认真 严谨 坚持
NET中如何发送广播消息
2008-10-25 12:41
$等待$
阅读(
1074
) 评论(
1
)
收藏
举报
从原理角度考虑,广播和单向定点发送没什么区别,献上一段小代码(来自msdn),基本很详细的说了如何广播式发送udp数据包:
Code
1
using
System;
2
using
System.Net;
3
using
System.Net.Sockets;
4
using
System.Text;
5
6
public
class
UDPMulticastSender
{
7
8
private
static
IPAddress GroupAddress
=
9
IPAddress.Parse(
"
224.168.100.2
"
);
10
private
static
int
GroupPort
=
11000
;
11
12
private
static
void
Send( String message)
{
13
UdpClient sender
=
new
UdpClient();
14
IPEndPoint groupEP
=
new
IPEndPoint(GroupAddress,GroupPort);
15
16
try
{
17
Console.WriteLine(
"
Sending datagram : {0}
"
, message);
18
byte
[] bytes
=
Encoding.ASCII.GetBytes(message);
19
20
sender.Send(bytes, bytes.Length, groupEP);
21
22
sender.Close();
23
24
}
catch
(Exception e)
{
25
Console.WriteLine(e.ToString());
26
}
27
28
}
29
30
public
static
int
Main(String[] args)
{
31
Send(args[
0
]);
32
33
return
0
;
34
}
35
36
37
38
刷新页面
返回顶部
About