UDP通讯接收案例(组播方式)
2016-12-15 15:24
399 查看
1.通讯类:
namespace AddMyUtility
{
public class UDPClientClass
{
public UDPClientClass(string groupIP, int Port, int timeOut)
{
_UdpClient = new UdpClient(Port);
_UdpClient.Client.ReceiveTimeout = timeOut;
_UdpClient.JoinMulticastGroup(IPAddress.Parse(groupIP));
_IPEndPoint = null;
}
public string _groupIP;
public int _port;
public int _timeOut;
public byte[] _Buffer;
public UdpClient _UdpClient;
public IPEndPoint _IPEndPoint;
public byte[] ReceivThreadEvetHandler()
{
try
{
byte[] buf = _UdpClient.Receive(ref _IPEndPoint);
_Buffer = buf;
return _Buffer;
}
catch
{
return new byte[0];
}
}
}
}
2.调用:
void ThirdUDPInit(string groupIP,int port,int timeOut)//第三组udp初始化
{
this._udpClientClass = new UDPClientClass(groupIP, port, timeOut);
}
public static string myAddGetIP()//获得主机IP
{
string strAddr = "";
try
{
string strHostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
foreach (IPAddress _ipaddress in ipEntry.AddressList)
{
if (_ipaddress.AddressFamily.ToString().ToLower() == "internetwork")
{
strAddr = _ipaddress.ToString();
break;
}
}
}
catch (System.Exception e)
{
}
return strAddr;
}
void MsgRecvThread_1()//2016.12.15
{
try
{
string text;
while (true)
{
try
{
byte[] content = this._udpClientClass.ReceivThreadEvetHandler();
if (content.Length > 0)
{
text = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]")
+ "收到外部消息:"
+ System.Text.Encoding.Default.GetString(content);
CommandOutputWindow.Output(text, ScriptMessageType.Command);
CommandOutputWindow.Output(System.Environment.NewLine, ScriptMessageType.Command);
}
}
catch
{ this._udpClientClass._UdpClient.Close();
this._udpClientClass._UdpClient = new UdpClient(_udpClientClass._port);
_udpClientClass._UdpClient.Client.ReceiveTimeout = _udpClientClass._timeOut;
_udpClientClass. _UdpClient.JoinMulticastGroup(IPAddress.Parse(_udpClientClass._groupIP));
_udpClientClass._IPEndPoint = null;
}
}
}
catch (Exception ex)
{
MessageOutputWindow.Output(ex.Message, ScriptMessageType.Error);
}
}
public void AddThread()//新加线程
{
Thread th = new Thread(new ThreadStart(MsgRecvThread_1));
th.Start();
}
namespace AddMyUtility
{
public class UDPClientClass
{
public UDPClientClass(string groupIP, int Port, int timeOut)
{
_UdpClient = new UdpClient(Port);
_UdpClient.Client.ReceiveTimeout = timeOut;
_UdpClient.JoinMulticastGroup(IPAddress.Parse(groupIP));
_IPEndPoint = null;
}
public string _groupIP;
public int _port;
public int _timeOut;
public byte[] _Buffer;
public UdpClient _UdpClient;
public IPEndPoint _IPEndPoint;
public byte[] ReceivThreadEvetHandler()
{
try
{
byte[] buf = _UdpClient.Receive(ref _IPEndPoint);
_Buffer = buf;
return _Buffer;
}
catch
{
return new byte[0];
}
}
}
}
2.调用:
void ThirdUDPInit(string groupIP,int port,int timeOut)//第三组udp初始化
{
this._udpClientClass = new UDPClientClass(groupIP, port, timeOut);
}
public static string myAddGetIP()//获得主机IP
{
string strAddr = "";
try
{
string strHostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
foreach (IPAddress _ipaddress in ipEntry.AddressList)
{
if (_ipaddress.AddressFamily.ToString().ToLower() == "internetwork")
{
strAddr = _ipaddress.ToString();
break;
}
}
}
catch (System.Exception e)
{
}
return strAddr;
}
void MsgRecvThread_1()//2016.12.15
{
try
{
string text;
while (true)
{
try
{
byte[] content = this._udpClientClass.ReceivThreadEvetHandler();
if (content.Length > 0)
{
text = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]")
+ "收到外部消息:"
+ System.Text.Encoding.Default.GetString(content);
CommandOutputWindow.Output(text, ScriptMessageType.Command);
CommandOutputWindow.Output(System.Environment.NewLine, ScriptMessageType.Command);
}
}
catch
{ this._udpClientClass._UdpClient.Close();
this._udpClientClass._UdpClient = new UdpClient(_udpClientClass._port);
_udpClientClass._UdpClient.Client.ReceiveTimeout = _udpClientClass._timeOut;
_udpClientClass. _UdpClient.JoinMulticastGroup(IPAddress.Parse(_udpClientClass._groupIP));
_udpClientClass._IPEndPoint = null;
}
}
}
catch (Exception ex)
{
MessageOutputWindow.Output(ex.Message, ScriptMessageType.Error);
}
}
public void AddThread()//新加线程
{
Thread th = new Thread(new ThreadStart(MsgRecvThread_1));
th.Start();
}
相关文章推荐
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- Linux下UDP的组播接收和发送的简单例子
- UDP通讯方式及编程实现步骤
- Linux网络编程一步一步学-UDP方式点对点通讯
- GCDAsyncUdpSocket 组播监听端口接收数据
- TCP和UDP网络通讯的区别及实现方式
- ios 接收 c# socket udp 组播
- ffmpeg笔记——UDP组播接收总结
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- UDP通讯方式及编程实现步骤
- TCP和UDP网络通讯的区别及实现方式
- UDP通讯方式及编程实现步骤
- linux socket接收、发送小工具(支持tcp、udp包(组播)的发送接收)
- Linux网络编程一步一步学-UDP方式广播通讯
- TCP和UDP网络通讯的区别及实现方式
- TCP和UDP网络通讯的区别及实现方式
- 从ffmpeg中抽取出来的UDP组播接收程序
- Linux网络编程一步一步学-UDP方式点对点通讯