UdpClient Class
Namespace: System.Net.Sockets
Assembly: System.Net.Sockets.dll
Overview
Provides support for the User Datagram Protocol (UDP).
The UdpClient class provides access to the UDP protocol, which is a connectionless protocol that sends datagrams without establishing a connection.
Table of Contents
Constructors
-
UdpClient()
Initializes a new instance of the
UdpClientclass. -
UdpClient(System.Int32 port)
Initializes a new instance of the
UdpClientclass and binds it to the specified local port.port- The local port to bind to.
-
UdpClient(System.Net.IPEndPoint localEP)
Initializes a new instance of the
UdpClientclass and binds it to the specified local endpoint.localEP- The local endpoint to bind to.
-
UdpClient(System.String hostname, System.Int32 port)
Initializes a new instance of the
UdpClientclass and connects it to the specified remote host and port.hostname- The hostname of the remote host.
port- The port of the remote host.
Properties
-
Available() : System.Int32
Gets the number of bytes available in the message in the network buffer.
- Return Value
- The number of bytes available.
-
Client() : System.Net.Sockets.Socket
Gets or sets the underlying
Socket.- Return Value
- The underlying
Socket.
-
ExclusiveAddressUse() : System.Boolean
Gets or sets a value that indicates whether the
UdpClientallows anotherUdpClientto be bound to the same address and port combination.- Return Value
trueif theUdpClientcan bind to the same address and port; otherwise,false.
Methods
-
Connect(System.Net.EndPoint remoteEP)
Connects the
UdpClientto a remote host and port.remoteEP- A
System.Net.EndPointthat represents the remote host and port to which to connect.
Remarks
For UDP, connecting associates the
UdpClientwith a specific remote host and port. Subsequent send and receive operations will be to and from that remote host and port. You can only have one remote host and port association perUdpClient.Exceptions
System.Net.Sockets.SocketException: An error occurred while accessing the socket.
-
ReceiveAsync() : System.Threading.Tasks.Task<System.Net.Sockets.UdpReceiveResult>
Asynchronously receives a datagram from the network.
- Return Value
- A task that represents the asynchronous operation. The value of the task contains a
UdpReceiveResultobject that contains the data received and the endpoint of the sender.
Remarks
This method does not block. It returns a
Taskthat can be used to await the result.Exceptions
System.Net.Sockets.SocketException: An error occurred while accessing the socket.System.ObjectDisposedException: TheUdpClienthas been closed.
-
SendAsync(System.Byte[] datagram, System.Net.EndPoint endPoint) : System.Threading.Tasks.Task<System.Int32>
Asynchronously sends a UDP datagram to a specified remote host and port.
datagram- A
System.Bytearray containing the data to send. endPoint- A
System.Net.EndPointthat represents the host and port to which to send the datagram.
- Return Value
- A task that represents the asynchronous operation. The value of the task contains the number of bytes sent.
Remarks
This method does not block. It returns a
Taskthat can be used to await the result.Exceptions
System.Net.Sockets.SocketException: An error occurred while accessing the socket.System.ObjectDisposedException: TheUdpClienthas been closed.
-
Close()
Releases the unmanaged resources used by the
UdpClientand closes the connection.Remarks
Calling
Closereleases all resources used by theUdpClientobject.
Extension Methods
This class also exposes several useful extension methods, including:
-
ReceiveAsync(this UdpClient udpClient) : Task<UdpReceiveResult>
Provides a task-based asynchronous version of the
Receivemethod. -
SendAsync(this UdpClient udpClient, Byte[] datagram, Int32 bytes, EndPoint remoteEP) : Task<Int32>
Asynchronously sends a UDP datagram to a specified remote host and port.
For detailed examples and further information, please refer to the UDP Client Sample.