UdpClient.BeginSend Method (Byte[], Int32, IPEndPoint, AsyncCallback, Object)

Sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginSend(
	byte[] datagram,
	int bytes,
	IPEndPoint endPoint,
	AsyncCallback requestCallback,
	Object state
)

Parameters

datagram
Type: System.Byte[]
A Byte array that contains the data to be sent.
bytes
Type: System.Int32
The number of bytes to send.
endPoint
Type: System.Net.IPEndPoint
The EndPoint that represents the destination for the data.
requestCallback
Type: System.AsyncCallback
An AsyncCallback delegate that references the method to invoke when the operation is complete.
state
Type: System.Object
A user-defined object that contains information about the send operation. This object is passed to the requestCallback delegate when the operation is complete.

Return Value

Type: System.IAsyncResult
An IAsyncResult object that references the asynchronous send.

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the Send method overloads.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example uses BeginSend to asynchronously send a server request.


public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
  UdpClient u = (UdpClient)ar.AsyncState;

  Console.WriteLine("number of bytes sent: {0}", u.EndSend(ar));
  messageSent = true;
}



static void SendMessage2(string server, string message)
{
  // create the udp socket
  UdpClient u = new UdpClient();
  Byte [] sendBytes = Encoding.ASCII.GetBytes(message);

  // resolve the server name
  IPHostEntry heserver = Dns.GetHostEntry(server);

  IPEndPoint e = new IPEndPoint(heserver.AddressList[0], listenPort);

  // send the message
  // the destination is defined by the IPEndPoint
  u.BeginSend(sendBytes, sendBytes.Length, e,
              new AsyncCallback(SendCallback), u);

  // Do some work while we wait for the send to complete. For 
  // this example, we'll just sleep
  while (!messageSent)
  {
    Thread.Sleep(100);
  }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft