注意:客户端和服务器实现基本一致,本地host和port和多播的host和port可以一样

(1)多播

1.将本地host加入多播组中,只有加入多播组的成员才能接受同组的节点发送的多播

MulticastOption mcastOption = new MulticastOption(IPAddress.Parse(MultiCastHost), IPAddress.Parse(localHost));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption);

2.将本地host移出多播组中

socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mcastOption);

3.多播生存时间 millisecond

socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 10);

发送信息指定MultiCastHost发送

(2)广播

开启广播

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);

发送信息指定255.255.255.255发送

参考:MSDN

    public class UdpServiceSocket
{
private readonly string broadCastHost = "255.255.255.255"; //接收数据事件
public Action<string> recvMessageEvent = null;
//发送结果事件
public Action<int> sendResultEvent = null; //接收缓存数组
private byte[] recvBuff = null;
//发送缓存数组
private byte[] sendBuff = null;
//用于发送数据的SocketAsyncEventArgs
private SocketAsyncEventArgs sendEventArg = null;
//用于接收数据的SocketAsyncEventArgs
private SocketAsyncEventArgs recvEventArg = null;
//监听socket
private Socket socket = null;
//用于socket发送和接收的缓存区大小
private int bufferSize = 1024;
//udp服务器绑定地址
private string localHost = "";
//udp服务器监听端口
private int localPort = 0;
//udp广播组地址
private string MultiCastHost = "";
//udp广播组端口
private int MultiCastPort = 0; /// <summary>
/// 构造函数
/// </summary>
/// <param name="bufferSize">用于socket发送和接受的缓存区大小</param>
public UdpServiceSocket()
{
//设置用于发送数据的SocketAsyncEventArgs
sendBuff = new byte[bufferSize];
sendEventArg = new SocketAsyncEventArgs();
sendEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
sendEventArg.SetBuffer(sendBuff, 0, bufferSize);
//设置用于接受数据的SocketAsyncEventArgs
recvBuff = new byte[bufferSize];
recvEventArg = new SocketAsyncEventArgs();
recvEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
recvEventArg.SetBuffer(recvBuff, 0, bufferSize);
} /// <summary>
/// 开启udp服务器,等待udp客户端数据(设置广播)
/// </summary>
public void Start(string localHost, int localPort)
{
if (string.IsNullOrEmpty(localHost))
throw new ArgumentNullException("localHost cannot be null");
if (localPort < 1 || localPort > 65535)
throw new ArgumentOutOfRangeException("localPort is out of range"); this.localHost = localHost;
this.localPort = localPort; try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //设置广播
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(localHost), localPort);
socket.Bind(endpoint);//设置监听地址和端口
StartRecvFrom();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 开启udp服务器,等待udp客户端数据(设置多播,广播)
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public void Start(string localHost, int localPort, string MultiCastHost, int MultiCastPort)
{
if (string.IsNullOrEmpty(localHost))
throw new ArgumentNullException("localHost cannot be null");
if (localPort < 1 || localPort > 65535)
throw new ArgumentOutOfRangeException("localPort is out of range"); if (string.IsNullOrEmpty(MultiCastHost))
throw new ArgumentNullException("MultiCastHost cannot be null");
if (MultiCastPort < 1 || MultiCastPort > 65535)
throw new ArgumentOutOfRangeException("MultiCastPort is out of range"); this.localHost = localHost;
this.localPort = localPort;
this.MultiCastHost = MultiCastHost;
this.MultiCastPort = MultiCastPort; try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //设置广播
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); //设置多播
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);
MulticastOption mcastOption = new MulticastOption(IPAddress.Parse(MultiCastHost), IPAddress.Parse(localHost));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption); IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(localHost), localPort);
socket.Bind(endpoint);//设置监听地址和端口
StartRecvFrom();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 开始接受udp客户端发送的数据
/// </summary>
private void StartRecvFrom()
{
recvEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
bool willRaiseEvent = socket.ReceiveFromAsync(recvEventArg);
if (!willRaiseEvent)
{
ProcessReceive(recvEventArg);
}
} /// <summary>
/// socket.sendAsync和socket.recvAsync的完成回调函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void IO_Completed(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.ReceiveFrom:
ProcessReceive(e);
break;
case SocketAsyncOperation.SendTo:
ProcessSend(e);
break;
default:
throw new ArgumentException("The last operation completed on the socket was not a receive or send");
}
} /// <summary>
/// 处理接收到的udp客户端数据
/// </summary>
/// <param name="e"></param>
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
if (recvMessageEvent != null)
//一定要指定GetString的长度
recvMessageEvent(Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred)); StartRecvFrom();
}
else
{
Restart();
}
} /// <summary>
/// 处理udp服务器发送的结果
/// </summary>
/// <param name="e"></param>
private void ProcessSend(SocketAsyncEventArgs e)
{
AsyncUserToken token = (AsyncUserToken)e.UserToken;
if (e.SocketError == SocketError.Success)
{
if (sendResultEvent != null)
sendResultEvent(e.BytesTransferred);
}
else
{
if (sendResultEvent != null)
sendResultEvent(e.BytesTransferred);
Restart();
}
} /// <summary>
/// 关闭udp服务器
/// </summary>
public void CloseSocket()
{
if (socket == null)
return; try
{
socket.Shutdown(SocketShutdown.Both);
}
catch { } try
{
socket.Close();
}
catch { }
} /// <summary>
/// 重新启动udp服务器
/// </summary>
public void Restart()
{
CloseSocket();
if (string.IsNullOrEmpty(MultiCastHost))
Start(localHost, localPort, MultiCastHost, MultiCastPort);
else
Start(localHost, localPort);
} /// <summary>
/// 发送广播
/// </summary>
/// <param name="message"></param>
public void SendMessageByBroadcast(string message)
{
if (socket == null)
throw new ArgumentNullException("socket cannot be null");
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException("message cannot be null"); byte[] buff = Encoding.UTF8.GetBytes(message);
if (buff.Length > bufferSize)
throw new ArgumentOutOfRangeException("message is out off range"); sendEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(broadCastHost), localPort);
buff.CopyTo(sendEventArg.Buffer, 0);
sendEventArg.SetBuffer(0, buff.Length);
bool willRaiseEvent = socket.SendToAsync(sendEventArg);
if (!willRaiseEvent)
{
ProcessSend(sendEventArg);
}
} /// <summary>
/// 发送单播
/// </summary>
/// <param name="message"></param>
public void SendMessageByUnicast(string message, string destHost, int destPort)
{
if (socket == null)
throw new ArgumentNullException("socket cannot be null");
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException("message cannot be null");
if (string.IsNullOrEmpty(destHost))
throw new ArgumentNullException("destHost cannot be null");
if (destPort < 1 || destPort > 65535)
throw new ArgumentOutOfRangeException("destPort is out of range"); byte[] buff = Encoding.UTF8.GetBytes(message);
if (buff.Length > bufferSize)
throw new ArgumentOutOfRangeException("message is out off range"); sendEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(destHost), destPort);
buff.CopyTo(sendEventArg.Buffer, 0);
sendEventArg.SetBuffer(0, buff.Length);
bool willRaiseEvent = socket.SendToAsync(sendEventArg);
if (!willRaiseEvent)
{
ProcessSend(sendEventArg);
}
} /// <summary>
/// 发送组播(多播)
/// </summary>
/// <param name="message"></param>
public void SendMessageByMulticast(string message)
{
if (socket == null)
throw new ArgumentNullException("socket cannot be null");
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException("message cannot be null");
if (string.IsNullOrEmpty(MultiCastHost))
throw new ArgumentNullException("MultiCastHost cannot be null");
if (MultiCastPort < 1 || MultiCastPort > 65535)
throw new ArgumentOutOfRangeException("MultiCastPort is out of range"); byte[] buff = Encoding.UTF8.GetBytes(message);
if (buff.Length > bufferSize)
throw new ArgumentOutOfRangeException("message is out off range"); sendEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(MultiCastHost), MultiCastPort);
buff.CopyTo(sendEventArg.Buffer, 0);
sendEventArg.SetBuffer(0, buff.Length);
bool willRaiseEvent = socket.SendToAsync(sendEventArg);
if (!willRaiseEvent)
{
ProcessSend(sendEventArg);
}
}
}

  

  

udp单播,广播,多播实现(ReceiveFromAsync,SendToAsync)的更多相关文章

  1. UDP单播、多播、广播

    一.UDP广播 广播使用的特殊的IP地址:最后一位是255时的IP地址是给广播预留的IP地址,如:192.168.88.255 广播UDP与单播UDP的区别就是IP地址不同,广播使用广播地址255.2 ...

  2. UDP 单播、广播和多播

    阅读目录(Content) 一.UDP广播 二.UDP多播 1.多播(组播)的概念 2.广域网的多播 三.UDP广播与单播 广播与单播的比较 使用UDP协议进行信息的传输之前不需要建议连接.换句话说就 ...

  3. UDP 单播、广播、多播

    一.UDP广播 广播UDP与单播UDP的区别就是IP地址不同,广播使用广播地址255.255.255.255,将消息发送到在同一广播网络上的每个主机.值得强调的是:本地广播信息是不会被路由器转发.当然 ...

  4. 以QQ举例 说明计算机网络中的一些概念区别(TCP与UDP,广播与单播)

    QQ 中的 广播与单播 今天简单地学习了一下 广播和多播(组播) 的知识.关于 单播和多播 的概念,可以用 QQ 中的一些例子来解释. 单播,就像 两个人聊QQ 一样,信息的接收和传递只在两个节点之间 ...

  5. UDP:rfc768/广播和多播/IGMP

    封装情况:

  6. netty的Udp单播、组播、广播实例+Java的Udp单播、组播、广播实例

    网络上缺乏netty的udp的单播.组播案例,经过一番学习总结之后终于把这两个案例调通,下面把这两个案例的代码放在这里分享一下. 首先推荐博文: http://colobu.com/2014/10/2 ...

  7. TCP/IP协议原理与应用笔记12:单播、多播和广播地址(目的地址)

    根据数据接收者的接收范围,将目的地址分为单播.多播.广播. 这里目的地址的划分主要针对的是 物理地址 和 IP地址,没有涉及到端口地址,因为主要针对标识通信节点的地址(物理地址 和 IP地址)而言,和 ...

  8. UDP单播和组播使用SO_REUSEADDR 测试结果

    UDP单播通信 一. 预置条件 A.B在同一台机器,网络中存在往A.B所在的机器的8888端口发送单播UDP数据 A:端口复用绑定在端口8888上 B:端口复用绑定在端口8888上操作步骤:(1)先启 ...

  9. iOS 利用Socket UDP协议广播机制的实现

    1.前言 什么是UDP协议广播机制? 举一个例. 比如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):"谁是张三" 假设它是张三,它就会回应你.在网络中也是一样的. ...

随机推荐

  1. kubernetes版本apiversion简单说明

    在使用yaml文件部署Deployment项目时,出现过 error: error validating "xx-Deployment.yaml": error validatin ...

  2. linux查看端口进程占用情况

    本文介绍linux如何查看端口被哪个进程占用的方法: 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 都可以查看指定端口被哪个进程占用的情况 [步骤一]lsof -i ...

  3. 【ARM-Linux开发】【Qt开发】Qt Creator自定义编译运行步骤

    原文:http://www.linuxidc.com/Linux/2015-04/115763.htm 一直用Qt Creator开发.无它,只是因为linux下C++ IDE选择不多.同时因为我抛弃 ...

  4. 解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 当项目依赖其他jar ...

  5. (三)mybatis 的使用(入门)

    目录 mybatis 的使用 -- 准备 mybatis 的使用 -- 搭建好工程结构 mybatis 的使用 -- 创建 sqlMapCnfig.xml 全局配置文件 mybatis 的使用 -- ...

  6. ubuntu 拨号上网

    如果没有安装的用户,可以使用 sudo apt-get install pppoe pppoeconf 然后配置上网 sudo pppoeconf 最后,使用 sudo pon dsl-provide ...

  7. centos7 宝塔php7安装mongodb扩展

    一.下载.解压源码 下载地址:https://pecl.php.net/package/mongodb wget -c https://pecl.php.net/get/mongodb-1.5.3.t ...

  8. python-day3(正式学习)

    执行python的两种方式 交互式 优点:运行一句执行一句,方便修改 缺点:关闭即消失,无法保存 命令行式 优点:能一直保存 缺点:代码全部写完才可以调试bug 以后尽量使用pycharm和jupyt ...

  9. 学习扩展kmp

    参考博客:https://blog.csdn.net/s_999999/article/details/89104957

  10. Python 线程&进程与协程

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...