收发都是啊~~~一个就能完成功能了.
组波还是单播都是使用Send函数.只是IPEndPoint这个参数不一样..何必用2个发呢....不同的发送方式.只需要修改这个参数就可以了啊
_localIpAddress = GetLocalIpAddress();
_msgSocket = new UdpClient(new IPEndPoint(IPAddress.Parse(_localIpAddress),_msgPort));
_msgSocket.EnableBroadcast = true;
_msgSocket.JoinMulticastGroup(_groupIP);
///
/// 组播消息
/// </summary>
/// </param>
public static void SendMsgToAll(string msg)
{
try
{
byte[] buffer = Encoding.Default.GetBytes(msg);
_msgSocket.Send(buffer, buffer.Length,_groupEP);
}
catch (Exception err)
{
throw err;
}
}
///
/// 发送消息给指定IP
/// </summary>
/// 要发送的消息</param>
/// 接收方的IP地址</param>
public static void SendMsgToOne(string msg,string ip)
{
try
{
byte[] buffer = Encoding.Default.GetBytes(msg);
_msgSocket.Send(buffer, buffer.Length, new IPEndPoint(IPAddress.Parse(ip), _msgPort));
}
catch (Exception err)
{
throw err;
}
}
只要替换最后一个参数.就实现了组播和单播.