Uredi

Deli z drugimi prek


Socket.ReceiveFrom Method

Definition

Receives a datagram and stores the source endpoint.

Overloads

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

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

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

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

ReceiveFrom(Span<Byte>, SocketFlags, EndPoint)

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Byte[], SocketFlags, EndPoint)

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Span<Byte>, SocketFlags, SocketAddress)

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Span<Byte>, EndPoint)

Receives a datagram into the data buffer and stores the endpoint.

ReceiveFrom(Byte[], EndPoint)

Receives a datagram into the data buffer and stores the endpoint.

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

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * int * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), size As Integer, socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

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

size
Int32

The number of bytes to receive.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

buffer is null.

-or-

remoteEP is null.

size is less than 0.

-or-

size is greater than the length of buffer.

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.

The Socket has been closed.

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

Examples

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

static void ReceiveFrom3()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );
   
   // Creates an IPEndPoint to capture the identity of the sending host.
   IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 );
   EndPoint^ senderRemote = safe_cast<EndPoint^>(sender);
   
   // Binding is required with ReceiveFrom calls.
   s->Bind( endPoint );

   array<Byte>^ msg = gcnew array<Byte>(256);
   Console::WriteLine(  "SWaiting to receive datagrams from client..." );
   // This call blocks. 
   s->ReceiveFrom( msg, msg->Length, SocketFlags::None, senderRemote );
   s->Close();
}
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 Shared Sub ReceiveFrom3() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    ' Creates an IPEndPoint to capture the identity of the sending host.
    Dim sender As New IPEndPoint(IPAddress.Any, 0)
    Dim senderRemote As EndPoint = CType(sender, EndPoint)
    
    ' Binding is required with ReceiveFrom calls.
    s.Bind(endPoint)
    
    Dim msg() As Byte = New [Byte](255) {}
    Console.WriteLine("Waiting to receive datagrams from client...")
    ' This call blocks. 
    s.ReceiveFrom(msg, msg.Length, SocketFlags.None, senderRemote)
    s.Close()

End Sub

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.

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

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.

Note

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 for a detailed description of the error.

Note

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

Note

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

See also

Applies to

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

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * int * int * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

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

offset
Int32

The position in the buffer parameter to store the received data.

size
Int32

The number of bytes to receive.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

buffer is null.

-or-

remoteEP is null.

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of the buffer minus the value of the offset parameter.

socketFlags is not a valid combination of values.

-or-

The LocalEndPoint property was not set.

-or-

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

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

static void ReceiveFrom4()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );
   
   // Creates an IpEndPoint to capture the identity of the sending host.
   IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 );
   EndPoint^ senderRemote = safe_cast<EndPoint^>(sender);
   
   // Binding is required with ReceiveFrom calls.
   s->Bind( endPoint );

   array<Byte>^ msg = gcnew array<Byte>(256);
   Console::WriteLine(  "SWaiting to receive datagrams from client..." );
   // This call blocks.  
   s->ReceiveFrom( msg, 0, msg->Length, SocketFlags::None, senderRemote );
   s->Close();
}
public static void ReceiveFrom4()
{
    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, 0, msg.Length, SocketFlags.None, ref senderRemote);
    s.Close();
}
Public Shared Sub ReceiveFrom4() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    ' Creates an IpEndPoint to capture the identity of the sending host.
    Dim sender As New IPEndPoint(IPAddress.Any, 0)
    Dim senderRemote As EndPoint = CType(sender, EndPoint)
    
    ' Binding is required with ReceiveFrom calls.
    s.Bind(endPoint)
    Dim msg() As Byte = New [Byte](255) {}
    Console.WriteLine("Waiting to receive datagrams from client...")
    ' This call blocks.  
    s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, senderRemote)
    s.Close()

End Sub

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

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

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 for a detailed description of the error.

Note

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

Note

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

See also

Applies to

ReceiveFrom(Span<Byte>, SocketFlags, EndPoint)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

public:
 int ReceiveFrom(Span<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (Span<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : Span<byte> * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Span(Of Byte), socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Span<Byte>

A span of bytes that is the storage location for received data.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

ReceiveFrom(Byte[], SocketFlags, EndPoint)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

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

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

buffer is null.

-or-

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

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

Examples

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

static void ReceiveFrom2()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );
   
   // Creates an IpEndPoint to capture the identity of the sending host.
   IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 );
   EndPoint^ senderRemote = safe_cast<EndPoint^>(sender);
   
   // Binding is required with ReceiveFrom calls.
   s->Bind( endPoint );

   array<Byte>^ msg = gcnew array<Byte>(256);
   Console::WriteLine( "Waiting to receive datagrams from client..." );
   // This call blocks. 
   s->ReceiveFrom( msg, SocketFlags::None, senderRemote );
   s->Close();
}
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,
        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, SocketFlags.None, ref senderRemote);
    s.Close();
}
Public Shared Sub ReceiveFrom2() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    ' Creates an IpEndPoint to capture the identity of the sending host.
    Dim sender As New IPEndPoint(IPAddress.Any, 0)
    Dim senderRemote As EndPoint = CType(sender, EndPoint)
    
    ' Binding is required with ReceiveFrom calls.
    s.Bind(endPoint)
    
    Dim msg() As Byte = New [Byte](255) {}
    Console.WriteLine("Waiting to receive datagrams from client...")
    ' This call blocks. 
    s.ReceiveFrom(msg, SocketFlags.None, senderRemote)
    s.Close()

End Sub

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.

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.

Note

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

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.

Note

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 for a detailed description of the error.

Note

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

Note

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

See also

Applies to

ReceiveFrom(Span<Byte>, SocketFlags, SocketAddress)

Source:
Socket.cs
Source:
Socket.cs

Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

public:
 int ReceiveFrom(Span<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::SocketAddress ^ receivedAddress);
public int ReceiveFrom (Span<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.SocketAddress receivedAddress);
member this.ReceiveFrom : Span<byte> * System.Net.Sockets.SocketFlags * System.Net.SocketAddress -> int
Public Function ReceiveFrom (buffer As Span(Of Byte), socketFlags As SocketFlags, receivedAddress As SocketAddress) As Integer

Parameters

buffer
Span<Byte>

A span of bytes that is the storage location for received data.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

receivedAddress
SocketAddress

A SocketAddress instance that gets updated with the value of the remote peer when this method returns.

Returns

The number of bytes received.

Exceptions

receivedAddress is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

ReceiveFrom(Span<Byte>, EndPoint)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Receives a datagram into the data buffer and stores the endpoint.

public:
 int ReceiveFrom(Span<System::Byte> buffer, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (Span<byte> buffer, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : Span<byte> * EndPoint -> int
Public Function ReceiveFrom (buffer As Span(Of Byte), ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Span<Byte>

A span of bytes that is the storage location for received data.

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

ReceiveFrom(Byte[], EndPoint)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Receives a datagram into the data buffer and stores the endpoint.

public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), ByRef remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

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

remoteEP
EndPoint

A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.

Returns

The number of bytes received.

Exceptions

buffer is null.

-or-

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

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

Examples

The following code example receives a connectionless datagram from a remote host.

static void ReceiveFrom1()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );
   
   // Creates an IPEndPoint to capture the identity of the sending host.
   IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 );
   EndPoint^ senderRemote = safe_cast<EndPoint^>(sender);
   
   // Binding is required with ReceiveFrom calls.
   s->Bind( endPoint );

   array<Byte>^ msg = gcnew array<Byte>(256);
   Console::WriteLine( "Waiting to receive datagrams from client..." );
   
   // This call blocks. 
   s->ReceiveFrom( msg, senderRemote );
   s->Close();
}
public static void ReceiveFrom1()
{
    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, ref senderRemote);
    s.Close();
}
Public Shared Sub ReceiveFrom1() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    ' Creates an IPEndPoint to capture the identity of the sending host.
    Dim sender As New IPEndPoint(IPAddress.Any, 0)
    Dim senderRemote As EndPoint = CType(sender, EndPoint)
    
    ' Binding is required with ReceiveFrom calls.
    s.Bind(endPoint)
    
    Dim msg() As Byte = New [Byte](255) {}
    Console.WriteLine("Waiting to receive datagrams from client...")
    
    ' This call blocks. 
    s.ReceiveFrom(msg, senderRemote)
    s.Close()

End Sub

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.

This overload only requires you to provide a receive buffer, and an EndPoint that represents the remote host. The buffer offset defaults to 0. The size defaults to the length of the buffer parameter and the socketFlags value defaults to None.

Note

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

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.

Note

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 for a detailed description of the error.

Note

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

Note

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

See also

Applies to