Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.ReceiveFrom Method (Byte[], Int32, SocketFlags, EndPoint)

Receives the specified number of bytes into the data buffer, using the specified SocketFlags, and stores the endpoint.

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

public int ReceiveFrom (
	byte[] buffer,
	int size,
	SocketFlags socketFlags,
	ref EndPoint remoteEP
)
public int ReceiveFrom (
	byte[] buffer, 
	int size, 
	SocketFlags socketFlags, 
	/** @ref */ EndPoint remoteEP
)
JScript does not support passing value-type arguments by reference.

Parameters

buffer

An array of type Byte that is the storage location for received data.

size

The number of bytes to receive.

socketFlags

A bitwise combination of the SocketFlags values.

remoteEP

An EndPoint, passed by reference, that represents the remote server.

Return Value

The number of bytes received.

Exception typeCondition

ArgumentNullException

buffer is a null reference (Nothing in Visual Basic).

-or-

remoteEP is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

size is less than 0.

-or-

size is greater than the length of buffer.

SocketException

socketFlags is not a valid combination of values.

-or-

The LocalEndPoint property was not set.

-or-

An operating system error occurs while accessing the Socket.

ObjectDisposedException

The Socket has been closed.

SecurityException

A caller in the call stack does not have the required permissions.

The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

This overload only requires you to provide a receive buffer, the number of bytes you want to receive, the necessary SocketFlags, and an EndPoint that represents the remote host. The buffer offset defaults to 0.

With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer. If the datagram you receive is larger than the size of buffer, the ReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. If you are using an unreliable protocol, the excess data will be lost. If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom method with a large enough buffer.

If no data is available for reading, the ReceiveFrom method will block until data is available. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

Although ReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept method. If you do not establish or accept a connection before calling the ReceiveFrom method, you will get a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom method. In either of these cases, the ReceiveFrom method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

With connection-oriented sockets, ReceiveFrom will read as much data as is available up to the number of bytes specified by the size parameter. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the ReceiveFrom method will complete immediately and return zero bytes.

NoteNote

Before calling ReceiveFrom, you must explicitly bind the Socket to a local endpoint using the Bind method. If you do not, ReceiveFrom will throw a SocketException. If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

NoteNote

The AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following code example receives a connectionless datagram from a remote host. The buffer size, and SocketFlags are passed to the ReceiveFrom method.

public static void ReceiveFrom3()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    // Creates an IPEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks. 
    s.ReceiveFrom(msg, msg.Length, SocketFlags.None, ref senderRemote);
    s.Close();
}

public static void ReceiveFrom3()
{
    IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName());
    IPEndPoint endPoint =
        new IPEndPoint(hostEntry.get_AddressList()[0], 11000);

    Socket s = new Socket(endPoint.get_Address().get_AddressFamily(),
        SocketType.Dgram, ProtocolType.Udp);
    // Creates an IPEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;
    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);

    ubyte msg[] = new ubyte[256];
    Console.WriteLine("Waiting to receive datagrams from client...");
    // This call blocks. 
    s.ReceiveFrom(msg, msg.length, SocketFlags.None, senderRemote);
    s.Close();
} //ReceiveFrom3

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft