c#的 UDP接收

    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
     
            Thread thread1 = new Thread(new ThreadStart(ReceiveData));
            thread1.Start();
        }
//UDP接收
        private int port = 5009;//2900
        private UdpClient udpClient;
        private void ReceiveData()
        {
            
            udpClient = new UdpClient(port);
            IPEndPoint remote = null;

            while (true)
            {
                try
                {

                    byte[] bytes = udpClient.Receive(ref remote);
//str字符串即是接收到的字符串
                    string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
//字符串中用"/b"分割
                    string[] sArray = str.Split('\b');

                
                }
                catch
                {
 
                    break;
                }
                finally
                {
                    udpClient.Close();
                }
            }
            
        }

發佈了25 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章