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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Receives a datagram 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,
	SocketFlags socketFlags,
	ref EndPoint remoteEP


Type: System.Byte[]

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

Type: System.Net.Sockets.SocketFlags

A bitwise combination of the SocketFlags values.

Type: System.Net.EndPoint

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

Return Value

Type: System.Int32

The number of bytes received.

Exception Condition

buffer is null.


remoteEP is null.


An error occurred when attempting to access the socket. See the Remarks section for more information.


The Socket has been closed.


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 necessary SocketFlags, and an EndPoint that represents the remote host. The offset defaults to 0 and the size defaults to the length of the buffer parameter.


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.

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. WhenAvailable 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 size of buffer. 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.


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.


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


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

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

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

    Socket s = new Socket(endPoint.Address.AddressFamily,

    // 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.

    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks. 
    s.ReceiveFrom(msg, SocketFlags.None, ref senderRemote);


for accepting connections from the network. Associated enumeration: Accept.

.NET Framework
Available since 1.1
Return to top