c sharp UdpClient类

也可以直接使用System.Net下的UdpClient进行通信

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
class Receive
{
      

      public static void ReceivedData(int port)
      {
          UdpClient client = new UdpClient(port);
          while (true)
          {
              IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
              byte[] data = client.Receive(ref anyIP);
              string text = Encoding.UTF8.GetString(data);
              //Console.WriteLine("text " + text);
              float data_t = Convert.ToSingle(text);
              Console.WriteLine("data_t " + data_t);
              //text.Toflo
          }
      }

      public static void Main()
      {
          ReceivedData(5064);
      }
  }
}
posted @ 2023-04-13 14:07  快乐码小农  阅读(31)  评论(0编辑  收藏  举报