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

Receives a datagram and stores the source endpoint.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReceiveFrom(Byte(), ByRef EndPoint) As Integer
[C#] public int ReceiveFrom(byte[], ref EndPoint);
[C++] public: int ReceiveFrom(unsigned char __gc[], EndPoint**);
[JScript] public function ReceiveFrom(Byte[], EndPoint) : int;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReceiveFrom(Byte(), SocketFlags, ByRef EndPoint) As Integer
[C#] public int ReceiveFrom(byte[], SocketFlags, ref EndPoint);
[C++] public: int ReceiveFrom(unsigned char __gc[], SocketFlags, EndPoint**);
[JScript] public function ReceiveFrom(Byte[], SocketFlags, EndPoint) : int;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReceiveFrom(Byte(), Integer, SocketFlags, ByRef EndPoint) As Integer
[C#] public int ReceiveFrom(byte[], int, SocketFlags, ref EndPoint);
[C++] public: int ReceiveFrom(unsigned char __gc[], int, SocketFlags, EndPoint**);
[JScript] public function ReceiveFrom(Byte[], int, SocketFlags, EndPoint) : int;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReceiveFrom(Byte(), Integer, Integer, SocketFlags, ByRef EndPoint) As Integer
[C#] public int ReceiveFrom(byte[], int, int, SocketFlags, ref EndPoint);
[C++] public: int ReceiveFrom(unsigned char __gc[], int, int, SocketFlags, EndPoint**);
[JScript] public function ReceiveFrom(Byte[], int, int, SocketFlags, EndPoint) : int;

Example

[Visual Basic, C#, C++] The following example receives a connectionless datagram from a remote host. The offset, buffer size, and SocketFlags are passed to the ReceiveFrom method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ReceiveFrom. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Shared Sub ReceiveFrom4()
    Dim hostEntry As IPHostEntry = Dns.Resolve(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 'ReceiveFrom4


[C#] 
public static void ReceiveFrom4()
{
    IPHostEntry hostEntry = Dns.Resolve(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();
}

[C++] 
static void ReceiveFrom4()
{
    IPHostEntry* hostEntry = Dns::Resolve(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 = __try_cast<EndPoint*>(sender);
    
    // Binding is required with ReceiveFrom calls.
    s->Bind(endPoint);
    Byte msg[]= new Byte[256];
    Console::WriteLine ("SWaiting to receive datagrams from client...");
    // This call blocks.  
    s->ReceiveFrom(msg, 0, msg.Length, SocketFlags::None, &senderRemote);
    s->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft