关于UdpClient的使用心得

  1. 连接方式1

    UdpClient udpClient = new UdpClient(11000);

    udpClient.Connect("192.168.0.5", 11000);

    Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

    udpClient.Send(sendBytes, sendBytes.Length);

     

    以上是一种连接方式,Connect和Send配合。

  2. 连接方式2

    UdpClient udpClientB = new UdpClient(11001);

    udpClientB.Send(sendBytes, sendBytes.Length, "192.168.0.5", 11000);

     

    直接用Send函数的这个重载就不用单独Connect()了。

  3. 关于构造函数

空的构造函数是不行的,比如想建立一个本地端点:

UdpClient udpClientC = new UdpClient();

这样不行,这样Client属性的LocalEndPoint=null。

原文地址:https://www.cnblogs.com/gmth/p/2446151.html