Share via


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

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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

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

Syntax

'Declaration
Public Function ReceiveFrom ( _
    buffer As Byte(), _
    offset As Integer, _
    size As Integer, _
    socketFlags As SocketFlags, _
    ByRef remoteEP As EndPoint _
) As Integer
public int ReceiveFrom(
    byte[] buffer,
    int offset,
    int size,
    SocketFlags socketFlags,
    ref EndPoint remoteEP
)
public:
int ReceiveFrom(
    array<unsigned char>^ buffer, 
    int offset, 
    int size, 
    SocketFlags socketFlags, 
    EndPoint^% remoteEP
)
member ReceiveFrom : 
        buffer:byte[] * 
        offset:int * 
        size:int * 
        socketFlags:SocketFlags * 
        remoteEP:EndPoint byref -> int 
public function ReceiveFrom(
    buffer : byte[], 
    offset : int, 
    size : int, 
    socketFlags : SocketFlags, 
    remoteEP : EndPoint
) : int

Parameters

  • buffer
    Type: array<System. . :: . .Byte> [] () [] []
    An array of type Byte that is the storage location for received data.

Return Value

Type: System. . :: . .Int32
The number of bytes received.

Remarks

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.

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

Note

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

.NET Framework Security

See Also

Reference

Socket Class

ReceiveFrom Overload

System.Net.Sockets Namespace