Επεξεργασία

Κοινή χρήση μέσω


Socket.Receive Method

Definition

Receives data from a bound Socket.

Overloads

Receive(Byte[], Int32, Int32, SocketFlags, SocketError)

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

Receive(Span<Byte>, SocketFlags, SocketError)

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

Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError)

Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

Receive(Byte[], Int32, SocketFlags)

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

Receive(Span<Byte>, SocketFlags)

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

Receive(Byte[], Int32, Int32, SocketFlags)

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

Receive(Byte[], SocketFlags)

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

Receive(Span<Byte>)

Receives data from a bound Socket into a receive buffer.

Receive(IList<ArraySegment<Byte>>)

Receives data from a bound Socket into the list of receive buffers.

Receive(Byte[])

Receives data from a bound Socket into a receive buffer.

Receive(IList<ArraySegment<Byte>>, SocketFlags)

Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

Receive(Byte[], Int32, Int32, SocketFlags, SocketError)

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

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

public:
 int Receive(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Receive (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Receive : byte[] * int * int * System.Net.Sockets.SocketFlags * SocketError -> int
Public Function Receive (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, ByRef errorCode As SocketError) As Integer

Parameters

buffer
Byte[]

An array of type Byte that is the storage location for the 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.

errorCode
SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes received.

Exceptions

buffer 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 buffer minus the value of the offset parameter.

The Socket has been closed.

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

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. An error occurred when attempting to access the socket. See Remarks below. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

If you are using a connection-oriented Socket, the Receive method 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 Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

Receive(Span<Byte>, SocketFlags, SocketError)

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

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

public:
 int Receive(Span<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Receive (Span<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Receive : Span<byte> * System.Net.Sockets.SocketFlags * SocketError -> int
Public Function Receive (buffer As Span(Of Byte), socketFlags As SocketFlags, ByRef errorCode As SocketError) As Integer

Parameters

buffer
Span<Byte>

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

socketFlags
SocketFlags

A bitwise combination of the enumeration values that specifies send and receive behaviors.

errorCode
SocketError

When this method returns, contains one of the enumeration values that defines error codes for the socket.

Returns

The number of bytes received.

Exceptions

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.

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

If you're using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you're using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. When the time-out value is exceeded, the Receive call will throw a SocketException. If you're in non-blocking mode, and there's no data available in the protocol stack buffer, the Receive 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.

If you're using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you're using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError)

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

Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

public:
 int Receive(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Receive : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags * SocketError -> int
Public Function Receive (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags, ByRef errorCode As SocketError) As Integer

Parameters

buffers
IList<ArraySegment<Byte>>

A list of ArraySegment<T>s of type Byte that contains the received data.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes received.

Exceptions

buffers is null.

-or-

buffers.Count is zero.

An error occurred while attempting to access the socket.

The Socket has been closed.

Remarks

This method reads data into the buffers parameter and returns the number of bytes successfully read. You can call from both connection-oriented and connectionless sockets.

This overload requires you to provide one or more receive buffers. The SocketFlags value defaults to None.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call throws a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive 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.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket,Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(Byte[], Int32, SocketFlags)

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

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

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

Parameters

buffer
Byte[]

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

size
Int32

The number of bytes to receive.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes received.

Exceptions

buffer is null.

size exceeds the size of buffer.

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 receives the data found into buffer, and specifies None for SocketFlags.

// Receive the host home page content and loop until all the data is received.
Int32 bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None );
strRetPage =  "Default HTML page on ";
strRetPage->Concat( server,  ":\r\n", ASCII->GetString( RecvBytes, 0, bytes ) );
while ( bytes > 0 )
{
   bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None );
   strRetPage->Concat( ASCII->GetString( RecvBytes, 0, bytes ) );
}

// Receive the host home page content and loop until all the data is received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

while (bytes > 0)
{
  bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
  strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}

' Receive the host home page content and loop until all the data is received.

'Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)

strRetPage = "Default HTML page on " + server + ":\r\n"
strRetPage = "Default HTML page on " + server + ":" + ControlChars.Lf + ControlChars.NewLine

Dim i As Integer

While bytes > 0

  bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)

  strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)

End While

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer, the number of bytes you want to receive, and the necessary SocketFlags.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, The Receive 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 your receive operation.

If you are using a connection-oriented Socket, the Receive method 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 Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(Span<Byte>, SocketFlags)

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

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

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

Parameters

buffer
Span<Byte>

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

socketFlags
SocketFlags

A bitwise combination of the enumeration values that specifies send and receive behaviors.

Returns

The number of bytes received.

Exceptions

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.

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

If you're using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you're using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. When the time-out value is exceeded, the Receive call will throw a SocketException. If you're in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive 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.

If you're using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you're using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost, and a SocketException is thrown.

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.

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

Receive(Byte[], Int32, Int32, SocketFlags)

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

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

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

Parameters

buffer
Byte[]

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

offset
Int32

The location in buffer to store the received data.

size
Int32

The number of bytes to receive.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes received.

Exceptions

buffer 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 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 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 specifies a data buffer, an offset, a size, and a socket flag before receiving data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
int SendReceiveTest4( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      
      // Blocks until send returns.
      int byteCount = server->Send( msg, 0, msg->Length, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes, 0, server->Available,
         SocketFlags::None );
      if ( byteCount > 0 )
      {
         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;
}
// 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 byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);

        // Get reply from the server.
        byteCount = server.Receive(bytes, 0, bytes.Length, SocketFlags.None);

        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes, 0, byteCount));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer, offset, message size, and socket flags.
Public Shared Function SendReceiveTest4(ByVal 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 byteCount As Integer = server.Send(msg, 0, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", byteCount)
        
        ' Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, SocketFlags.None)
        
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest4

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. An error occurred when attempting to access the socket. See Remarks below. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

If you are using a connection-oriented Socket, the Receive method 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 Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(Byte[], SocketFlags)

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

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

public:
 int Receive(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags);
public int Receive (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags);
member this.Receive : byte[] * System.Net.Sockets.SocketFlags -> int
Public Function Receive (buffer As Byte(), socketFlags As SocketFlags) 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.

Returns

The number of bytes received.

Exceptions

buffer 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 specifies a data buffer, and SocketFlags for receiving data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
int SendReceiveTest2( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      // Blocks until send returns.
      int byteCount = server->Send( msg, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes, SocketFlags::None );
      if ( byteCount > 0 )
      {
         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;
}
// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
public static int SendReceiveTest2(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);

        // Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None);
        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer and socket flags.
Public Shared Function SendReceiveTest2(ByVal 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 byteCount As Integer = server.Send(msg, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", byteCount)
        
        ' Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None)
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest2

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer and the necessary SocketFlags. The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive 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 Receive 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 your receive operation.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(Span<Byte>)

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

Receives data from a bound Socket into a receive buffer.

public:
 int Receive(Span<System::Byte> buffer);
public int Receive (Span<byte> buffer);
member this.Receive : Span<byte> -> int
Public Function Receive (buffer As Span(Of Byte)) As Integer

Parameters

buffer
Span<Byte>

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

Returns

The number of bytes received.

Exceptions

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.

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

If you're using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection before calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you're using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. When the time-out value is exceeded, the Receive call will throw a SocketException. If you're in non-blocking mode, and there's no data available in the protocol stack buffer, the Receive 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.

If you're using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you're using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost, and a SocketException is thrown.

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.

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

Receive(IList<ArraySegment<Byte>>)

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

Receives data from a bound Socket into the list of receive buffers.

public:
 int Receive(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public int Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.Receive : System.Collections.Generic.IList<ArraySegment<byte>> -> int
Public Function Receive (buffers As IList(Of ArraySegment(Of Byte))) As Integer

Parameters

buffers
IList<ArraySegment<Byte>>

A list of ArraySegment<T>s of type Byte that contains the received data.

Returns

The number of bytes received.

Exceptions

The buffer parameter is null.

An error occurred while attempting to access the socket.

The Socket has been closed.

Remarks

This method reads data into the buffers parameter and returns the number of bytes successfully read. You can call from both connection-oriented and connectionless sockets.

This overload requires you to provide one or more receive buffers.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive 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.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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 This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Applies to

Receive(Byte[])

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

Receives data from a bound Socket into a receive buffer.

public:
 int Receive(cli::array <System::Byte> ^ buffer);
public int Receive (byte[] buffer);
member this.Receive : byte[] -> int
Public Function Receive (buffer As Byte()) As Integer

Parameters

buffer
Byte[]

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

Returns

The number of bytes received.

Exceptions

buffer 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 data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer.
int SendReceiveTest1( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      // Blocks until send returns.
      int byteCount = server->Send( msg );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes );
      if ( byteCount > 0 )
      {
         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;
}
// Displays sending with a connected socket
// using the overload that takes a buffer.
public static int SendReceiveTest1(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);
        Console.WriteLine("Sent {0} bytes.", i);

        // Get reply from the server.
        i = server.Receive(bytes);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer.
Public Shared Function SendReceiveTest1(ByVal 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)
        Console.WriteLine("Sent {0} bytes.", i)
        
        ' Get reply from the server.
        i = server.Receive(bytes)
        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 'SendReceiveTest1

Remarks

The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.

This overload only requires you to provide a receive buffer. The buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive 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.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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

Receive(IList<ArraySegment<Byte>>, SocketFlags)

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

Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

public:
 int Receive(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
public int Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.Receive : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags -> int
Public Function Receive (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags) As Integer

Parameters

buffers
IList<ArraySegment<Byte>>

A list of ArraySegment<T>s of type Byte that contains the received data.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes received.

Exceptions

buffers is null.

-or-

buffers.Count is zero.

An error occurred while attempting to access the socket.

The Socket has been closed.

Examples

The following code example demonstrates how to receive data on a connected Socket.


// Build the buffers for the receive.
List<ArraySegment<Byte> >^ receiveBuffers = 
    gcnew List<ArraySegment<Byte> >(2);

array<Byte>^ bigBuffer = gcnew array<Byte>(1024);

// Specify the first buffer segment (2 bytes, starting 
// at the 4th element of bigBuffer)
receiveBuffers->Add(ArraySegment<Byte>(bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
receiveBuffers->Add(
    ArraySegment<Byte>(bigBuffer, 20, 500));

tcpSocket->Receive(receiveBuffers);

Console::WriteLine("{0}", 
    asciiEncoding->GetString(bigBuffer));

// Build the buffers for the receive.
List<ArraySegment<byte>> recvBuffers =
                         new List<ArraySegment<byte>>(2);

byte[] bigBuffer = new byte[1024];

// Specify the first buffer segment (2 bytes, starting
// at the 4th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>(bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>(bigBuffer, 20, 500));

int bytesReceived = mySocket.Receive(recvBuffers);

Console.WriteLine("{0}", ASCII.GetString(bigBuffer));

Remarks

This method reads data into the buffers parameter and returns the number of bytes successfully read. You can call from both connection-oriented and connectionless sockets.

This overload requires you to provide one or more receive buffers. The SocketFlags value defaults to None.

If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call throws a SocketException. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive 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.

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

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

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