UDP通讯接收案例(组播方式)
生活随笔
收集整理的這篇文章主要介紹了
UDP通讯接收案例(组播方式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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();
???????
??????? }
{
??? 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();
???????
??????? }
總結
以上是生活随笔為你收集整理的UDP通讯接收案例(组播方式)的全部內容,希望文章能夠幫你解決所遇到的問題。