Provides User Datagram Protocol (UDP) network services.
Assembly: System (in System.dll)
Thetype exposes the following members.
|UdpClient||Initializes a new instance of the class.|
|UdpClient(AddressFamily)||Initializes a new instance of the class.|
|UdpClient(Int32)||Initializes a new instance of the class and binds it to the local port number provided.|
|UdpClient(IPEndPoint)||Initializes a new instance of the class and binds it to the specified local endpoint.|
|UdpClient(Int32, AddressFamily)||Initializes a new instance of the class and binds it to the local port number provided.|
|UdpClient(String, Int32)||Initializes a new instance of the class and establishes a default remote host.|
|Active||Gets or sets a value indicating whether a default remote host has been established.|
|Available||Gets the amount of data received from the network that is available to read.|
|Client||Gets or sets the underlying network Socket.|
|DontFragment||Gets or sets a Boolean value that specifies whether the allows Internet Protocol (IP) datagrams to be fragmented.|
|EnableBroadcast||Gets or sets a Boolean value that specifies whether the may send or receive broadcast packets.|
|ExclusiveAddressUse||Gets or sets a Boolean value that specifies whether the allows only one client to use a port.|
|MulticastLoopback||Gets or sets a Boolean value that specifies whether outgoing multicast packets are delivered to the sending application.|
|Ttl||Gets or sets a value that specifies the Time to Live (TTL) value of Internet Protocol (IP) packets sent by the .|
|AllowNatTraversal||Enables or disables Network Address Translation (NAT) traversal on a instance.|
|BeginReceive||Receives a datagram from a remote host asynchronously.|
|BeginSend(Byte(), Int32, AsyncCallback, Object)||Sends a datagram to a remote host asynchronously. The destination was specified previously by a call to Connect.|
|BeginSend(Byte(), Int32, IPEndPoint, AsyncCallback, Object)||Sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.|
|BeginSend(Byte(), Int32, String, Int32, AsyncCallback, Object)||Sends a datagram to a destination asynchronously. The destination is specified by the host name and port number.|
|Close||Closes the UDP connection.|
|Connect(IPEndPoint)||Establishes a default remote host using the specified network endpoint.|
|Connect(IPAddress, Int32)||Establishes a default remote host using the specified IP address and port number.|
|Connect(String, Int32)||Establishes a default remote host using the specified host name and port number.|
|Dispose||Releases the unmanaged resources used by the and optionally releases the managed resources.|
|DropMulticastGroup(IPAddress)||Leaves a multicast group.|
|DropMulticastGroup(IPAddress, Int32)||Leaves a multicast group.|
|EndReceive||Ends a pending asynchronous receive.|
|EndSend||Ends a pending asynchronous send.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|JoinMulticastGroup(IPAddress)||Adds a to a multicast group.|
|JoinMulticastGroup(Int32, IPAddress)||Adds a to a multicast group.|
|JoinMulticastGroup(IPAddress, Int32)||Adds a to a multicast group with the specified Time to Live (TTL).|
|JoinMulticastGroup(IPAddress, IPAddress)||Adds a to a multicast group.|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|Receive||Returns a UDP datagram that was sent by a remote host.|
|Send(Byte(), Int32)||Sends a UDP datagram to a remote host.|
|Send(Byte(), Int32, IPEndPoint)||Sends a UDP datagram to the host at the specified remote endpoint.|
|Send(Byte(), Int32, String, Int32)||Sends a UDP datagram to a specified port on a specified remote host.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
The class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways:
Create an instance of the class using the remote host name and port number as parameters.
Create an instance of the class and then call the Connect method.
You can use any of the send methods provided in the to send data to a remote device. Use the Receive method to receive data from remote hosts.
methods also allow you to send and receive multicast datagrams. Use the JoinMulticastGroup method to subscribe a to a multicast group. Use the DropMulticastGroup method to unsubscribe a from a multicast group.
The following example establishes a connection using the host name www.contoso.com on port 11000. A small string message is sent to two separate remote host machines. The Receive method blocks execution until a message is received. Using the IPEndPoint passed to Receive, the identity of the responding host is revealed.
' This constructor arbitrarily assigns the local port number. Dim udpClient As New UdpClient(11000) Try udpClient.Connect("www.contoso.com", 11000) ' Sends a message to the host to which you have connected. Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?") udpClient.Send(sendBytes, sendBytes.Length) ' Sends message to a different host using optional hostname and port parameters. Dim udpClientB As New UdpClient() udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000) ' IPEndPoint object will allow us to read datagrams sent from any source. Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0) ' UdpClient.Receive blocks until a message is received from a remote host. Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint) Dim returnData As String = Encoding.ASCII.GetString(receiveBytes) ' Which one of these two hosts responded? Console.WriteLine(("This is the message you received " + _ returnData.ToString())) Console.WriteLine(("This message was sent from " + _ RemoteIpEndPoint.Address.ToString() + _ " on their port number " + _ RemoteIpEndPoint.Port.ToString())) udpClient.Close() udpClientB.Close() Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub
To establish an outgoing connection or accept an incoming request.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.