public class UdpClient : IDisposable
|
You can use any of the send methods provided in the UdpClient to send data to a remote device. Use the UdpClient.Receive method to receive data from remote devices
UdpClient methods allow you to also receive multicasted datagrams. Use UdpClient.JoinMulticastGroup and UdpClient.DropMulticastGroup to associate and disassociate a UdpClient with a multicast group.
// This constructor arbitrarily assigns the local port number. UdpClient udpClient = new UdpClient(); try{ udpClient.Connect("www.contoso.com", 11000); // Sends a message to the host to which you have connected. Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?"); udpClient.Send(sendBytes, sendBytes.Length); // Sends a message to a different host using optional hostname and port parameters. UdpClient udpClientB = new UdpClient(); udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000); //IPEndPoint object will allow us to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // Uses the IPEndPoint object to determine which 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 (Exception e ) { Console.WriteLine(e.ToString()); }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the UdpClient class. |
ctor #2 | Overloaded:.ctor(int port) Initializes a new instance of the UdpClient class that communicates on a specified port. |
ctor #3 | Overloaded:.ctor(IPEndPoint localEP) Initializes a new instance of the UdpClient class that communicates on a specified local endpoint. |
ctor #4 | Overloaded:.ctor(string hostname, int port) Initializes a new instance of the UdpClient class and connects to a specified remote host on a specified port. |
Close | Closes the UDP connection. |
Connect | Overloaded:Connect(IPEndPoint endPoint) Connects the client to a remote UDP host using the specified remote network endpoint. |
Connect | Overloaded:Connect(IPAddress addr, int port) Connects the client to a remote UDP host using the specified IP Address and port number. |
Connect | Overloaded:Connect(string hostname, int port) Establishes a connection to the specified port on the specified remote host. |
DropMulticastGroup | Leaves a multicast group. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
JoinMulticastGroup | Overloaded:JoinMulticastGroup(IPAddress multicastAddr) Adds a UdpClient to a multicast group. |
JoinMulticastGroup | Overloaded:JoinMulticastGroup(IPAddress multicastAddr, int timeToLive) Adds a UdpClient to a multicast group with the specified Time to Live (TTL). |
Receive | Returns a UDP datagram that was sent by a remote host. |
Send | Overloaded:Send(byte[] dgram, int bytes) Sends a UDP datagram to a remote host. |
Send | Overloaded:Send(byte[] dgram, int bytes, IPEndPoint endPoint) Sends a UDP datagram to the host at the remote endpoint. |
Send | Overloaded:Send(byte[] dgram, int bytes, string hostname, int port) Sends a UDP datagram to a specified port on a specified remote host. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Active | Read-write Gets or sets a value indicating whether a connection to a remote host has been made. |
Client | Read-write Gets or sets the underlying network socket. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public UdpClient(); |
//Creates an instance of the UdpClient class using the default constructor. UdpClient udpClient = new UdpClient();
public UdpClient( |
port
Exception Type | Condition |
---|---|
ArgumentException | The port parameter is greater than IPEndPoint.MaxPort or less than IPEndPoint.MinPort. |
//Creates an instance of the UdpClient class to listen on // the default interface using a particular port. try{ UdpClient udpClient = new UdpClient(11000); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
public UdpClient( |
localEP
Exception Type | Condition |
---|---|
ArgumentNullException | The localEP parameter is null. |
//Creates an instance of the UdpClient class using a local endpoint. IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); try{ UdpClient udpClient = new UdpClient(ipLocalEndPoint); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
hostname
port
Exception Type | Condition |
---|---|
ArgumentNullException | The hostname parameter is null. |
ArgumentException | The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort. |
SocketException | An error occurred while connecting to the remote host. |
//Creates an instance of the UdpClient class with a remote host name and a port number. try{ UdpClient udpClient = new UdpClient("www.contoso.com",11000); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
protected bool Active {get; set;}
|
MyUdpClientDerivedClass
verifies that the connection is active before obtaining the underlying Socket.// This derived class demonstrate the use of three protected methods belonging to the UdpClient class. public class MyUdpClientDerivedClass : UdpClient{ public MyUdpClientDerivedClass() : base(){ } public void UsingProtectedMethods(){ //Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established. if (this.Active){ //Calls the protected Client property belonging to the UdpClient base class. Socket s = this.Client; //Uses the Socket returned by Client to set an option that is not available using UdpClient. s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); } } }
protected Socket Client {get; set;}
|
MyUdpClientDerivedClass
obtains the underlying Socket to enable broadcasting.// This derived class demonstrate the use of three protected methods belonging to the UdpClient class. public class MyUdpClientDerivedClass : UdpClient{ public MyUdpClientDerivedClass() : base(){ } public void UsingProtectedMethods(){ //Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established. if (this.Active){ //Calls the protected Client property belonging to the UdpClient base class. Socket s = this.Client; //Uses the Socket returned by Client to set an option that is not available using UdpClient. s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); } } }
public void Close(); |
Exception Type | Condition |
---|---|
SocketException | An error occurred while closing the socket. |
// Closes the UDP client by calling the public method Close(). udpClient.Close();
public void Connect( |
endPoint
Exception Type | Condition |
---|---|
SocketException | An error occurred while connecting to the remote host. |
//Uses a remote endpoint to establish a socket connection. UdpClient udpClient = new UdpClient(); IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004); try{ udpClient.Connect(ipEndPoint); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
addr
port
Exception Type | Condition |
---|---|
ObjectDisposedException | The underlying Socket has been closed. |
ArgumentNullException | The address parameter is null. |
ArgumentOutOfRangeException | The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort. |
SocketException | An error occurred while connecting to the remote host. |
//Uses the IP address and port number to establish a socket connection. UdpClient udpClient = new UdpClient(); IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0]; try{ udpClient.Connect(ipAddress, 11003); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
hostname
port
Exception Type | Condition |
---|---|
ObjectDisposedException | The underlying Socket has been closed. |
ArgumentException | The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort. |
SocketException | An error occurred while connecting to the remote host. |
//Uses a host name and port number to establish a socket connection. UdpClient udpClient = new UdpClient(); try{ udpClient.Connect("www.contoso.com", 11002); } catch (Exception e ) { Console.WriteLine(e.ToString()); }
public void DropMulticastGroup( |
multicastAddr
Exception Type | Condition |
---|---|
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | The Socket was unable to leave the multicast group. |
// Informs the server that you want to remove yourself from the multicast client list. try{ udpClient.DropMulticastGroup(multicastIpAddress); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
~UdpClient(); |
public virtual int GetHashCode(); |
public Type GetType(); |
public void JoinMulticastGroup( |
multicastAddr
Exception Type | Condition |
---|---|
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | The socket was unable to join the multicast group. |
UdpClient udpClient = new UdpClient(); IPAddress multicastIpAddress = Dns.Resolve("MulticastGroupName").AddressList[0]; try{ udpClient.JoinMulticastGroup(multicastIpAddress); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
multicastAddr
timeToLive
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The TTL provided is not between 0 and 255 |
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | The Socket was unable to join the multicast group. |
UdpClient udpClient = new UdpClient(); // Creates an IPAddress to use to join and drop the multicast group. IPAddress multicastIpAddress = IPAddress.Parse("239.255.255.255"); try{ // The packet dies after 50 router hops. udpClient.JoinMulticastGroup(multicastIpAddress, 50); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
protected object MemberwiseClone(); |
remoteEP
Exception Type | Condition |
---|---|
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | An error occurred while reading the Socket. |
//Creates a UdpClient for reading incoming data. UdpClient receivingUdpClient = new UdpClient(); //Creates an IPEndPoint to record the IP Address and port number of the sender. // The IPEndPoint will allow you to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); try{ // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); 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()); } catch ( Exception e ){ Console.WriteLine(e.ToString()); }
dgram
bytes
Exception Type | Condition |
---|---|
ArgumentException | The dgram parameter is null. |
InvalidOperationException | UdpClient is already connected to a remote host. |
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | An error occurred while sending data to the Socket. |
UdpClient udpClient = new UdpClient("www.contoso.com", 11000); Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there"); try{ udpClient.Send(sendBytes, sendBytes.Length); } catch ( Exception e ){ Console.WriteLine( e.ToString()); }
public int Send( |
dgram
bytes
endPoint
Exception Type | Condition |
---|---|
ArgumentException | The dgram parameter is null. |
InvalidOperationException | UdpClient is already connected to a remote host. |
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | An error occurred while sending data to the Socket. |
UdpClient udpClient = new UdpClient(); IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004); Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?"); try{ udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint); } catch ( Exception e ){ Console.WriteLine(e.ToString()); }
dgram
bytes
hostname
port
Exception Type | Condition |
---|---|
ArgumentException | The dgram parameter is null. |
InvalidOperationException | UdpClient is already connected to a remote host. |
ObjectDisposedException | The underlying Socket has been closed. |
SocketException | An error occurred while sending data to the Socket. |
If you UdpClient.Connect to a remote host before calling this method, UdpClient will throw an exception. If you have already established a default remote host using UdpClient.Connect, consider one of the following before using UdpClient.Send:
UdpClient udpClient = new UdpClient(); Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there"); try{ udpClient.Send(sendBytes, sendBytes.Length, "www.contoso.com", 11000); } catch ( Exception e ){ Console.WriteLine(e.ToString()); }
public virtual string ToString(); |