Export (0) Print
Expand All

Socket.Receive Method

Receives data from a bound Socket.

Overload List

Receives data from a bound Socket into a receive buffer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Receive(Byte()) As Integer
[C#] public int Receive(byte[]);
[C++] public: int Receive(unsigned char __gc[]);
[JScript] public function Receive(Byte[]) : int;

Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

Supported by the .NET Compact Framework.

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

Receives the specified number of bytes of data from a bound Socket into a receive buffer, using the specified SocketFlags.

Supported by the .NET Compact Framework.

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

Receives the specified number of bytes from a bound Socket into the specified offset position of the receive buffer, using the specified SocketFlags.

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] The following example specifies a data buffer, an offset, a size, and a socket flag before receiving data on a connected Socket.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Receive. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Displays sending with a connected socket
' using the overload that takes a buffer, offset, message size, and socket flags.
Public Shared Function SendReceiveTest4(server As Socket) As Integer
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim i As Integer = server.Send(msg, 0, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", i)
        
        ' Get reply from the server.
        server.Receive(bytes, 0, server.Available, SocketFlags.None)
        Console.WriteLine(Encoding.UTF8.GetString(bytes))
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0
End Function 'SendReceiveTest4


[C#] 
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        server.Receive(bytes, 0, server.Available, SocketFlags.None);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}

[C++] 
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
int SendReceiveTest4(Socket* server)
{
    Byte msg[]= Encoding::UTF8->GetBytes(S"This is a test");
    Byte bytes[] = new Byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server->Send(msg, 0, msg.Length, SocketFlags::None);
        Console::WriteLine(S"Sent {0} bytes.", i.ToString());
        
        // Get reply from the server.
        server->Receive(bytes, 0, server->Available, SocketFlags::None);
        Console::WriteLine(Encoding::UTF8->GetString(bytes));
    }
    catch (SocketException* e)
    {
        Console::WriteLine("{0} Error code: {1}.", e->Message, e->ErrorCode.ToString());
        return ( e->ErrorCode);
    }
    return 0;
}

[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

Show:
© 2014 Microsoft