using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace UDP_Client.UDP { class UDP_Client { private const int listenPort = 6800; private const String IP = "255.255.255.255"; private UdpClient listener= new UdpClient(listenPort); private bool action_flag; public UDP_Get_message_action action = null; public void StartListener() { Thread thread = new Thread(listener_action); thread.Start(); } //讀取的資料 private void listener_action() { IPAddress iPAddress = IPAddress.Parse(IP); IPEndPoint groupEP = new IPEndPoint(iPAddress, listenPort); byte[] bytes; String temp; action_flag = true; try { while (true) { bytes = listener.Receive(ref groupEP); temp=Encoding.ASCII.GetString(bytes, 0, bytes.Length); if (action != null) { action.onGetMessage(temp); } } } catch (SocketException e) { Console.WriteLine(e); } finally { listener.Close(); } } public void close() { if (action_flag == true) { action_flag = false; try { listener.Close(); } catch (SocketException e) { } } } } }
2022年12月16日
WPF UDP 客戶端exmplae
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言