Breyta

Deila með


Socket.Send Method

Definition

Sends data to a connected Socket.

Overloads

Send(ReadOnlySpan<Byte>, SocketFlags, SocketError)

Sends data to a connected Socket using the specified SocketFlags.

Send(ReadOnlySpan<Byte>)

Sends data to a connected Socket.

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

Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

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

Sends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

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

Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

Send(Byte[], Int32, SocketFlags)

Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.

Send(IList<ArraySegment<Byte>>)

Sends the set of buffers in the list to a connected Socket.

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

Sends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

Send(Byte[], SocketFlags)

Sends data to a connected Socket using the specified SocketFlags.

Send(ReadOnlySpan<Byte>, SocketFlags)

Sends data to a connected Socket using the specified SocketFlags.

Send(Byte[])

Sends data to a connected Socket.

Send(ReadOnlySpan<Byte>, SocketFlags, SocketError)

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

Sends data to a connected Socket using the specified SocketFlags.

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

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

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 sent to the Socket.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

This overload requires a buffer that contains the data you want to send. The SocketFlags value defaults to 0, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.

If you're using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you're using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you're using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you don't use the SendTo method, you'll have to call Connect before each call to Send. You can use SendTo even after you've established a default remote host with Connect. You can also change the default remote host before calling Send by making another call to Connect.

If you're using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There's also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you've 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.

Important

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

See also

Applies to

Send(ReadOnlySpan<Byte>)

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

Sends data to a connected Socket.

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

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

Returns

The number of bytes sent to the Socket.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

This overload requires a buffer that contains the data you want to send. The SocketFlags value defaults to 0, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.

If you're using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you're using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you're using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you don't use the SendTo method, you'll have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host before calling Send by making another call to Connect.

If you're using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There's also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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.

Important

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

See also

Applies to

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

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

Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

public:
 int Send(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Send (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Send : byte[] * int * int * System.Net.Sockets.SocketFlags * SocketError -> int
Public Function Send (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 contains the data to be sent.

offset
Int32

The position in the data buffer at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes sent to the Socket.

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-

An operating system error occurs while accessing the Socket.

The Socket has been closed.

Examples

The following code example specifies the data buffer, an offset, a size, and SocketFlags for sending data to 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

Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

In this overload, if you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you must call Connect before calling this method or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. If you do not use SendTo, you will have to call Connect before each call to Send. It is okay to use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

You must also be sure that the size does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and Send will throw a SocketException.

If you are using a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

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

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

Sends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags * SocketError -> int
Public Function Send (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 data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes sent to the Socket.

Exceptions

buffers is null.

buffers is empty.

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

This overload requires at least one buffer that contains the data you want to send. The SocketFlags value defaults to 0. If you specify the DontRoute flag as the socketFlags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you will have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In non-blocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

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

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

Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

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

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

offset
Int32

The position in the data buffer at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes sent to the Socket.

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-

An operating system error occurs while accessing the Socket.

The Socket has been closed.

Examples

The following code example specifies the data buffer, an offset, a size, and SocketFlags for sending data to 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

Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

In this overload, if you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you must call Connect before calling this method or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. If you do not use SendTo, you will have to call Connect before each call to Send. It is okay to use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

You must also be sure that the size does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and Send will throw a SocketException.

If you are using a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

Send(Byte[], Int32, SocketFlags)

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

Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.

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

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes sent to the Socket.

Exceptions

buffer is null.

size is less than 0 or exceeds the size of the buffer.

socketFlags is not a valid combination of values.

-or-

An operating system error occurs while accessing the socket.

The Socket has been closed.

Examples

The following code example sends the data found in buffer, and specifies None for SocketFlags.

// Displays sending with a connected socket
// using the overload that takes a buffer, message size, and socket flags.
int SendReceiveTest3( 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 i = server->Send( msg, msg->Length, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", i.ToString() );
      
      // Get reply from the server.
      int byteCount = server->Receive( bytes, 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, message size, and socket flags.
public static int SendReceiveTest3(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, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);

        // Get reply from the server.
        int byteCount = server.Receive(bytes, 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, message size, and socket flags.
Public Shared Function SendReceiveTest3(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, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", i)
        
        ' Get reply from the server.
        Dim byteCount As Integer = server.Receive(bytes, 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 'SendReceiveTest3

Remarks

Send synchronously sends data to the remote host established in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

This overload requires a buffer that contains the data you want to send, the number of bytes you want to send, and a bitwise combination of any SocketFlags. If you specify the DontRoute flag as the socketflags parameter, the data you're sending won't be routed.

If you're using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you're using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you're using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you don'tuse the SendTo method, you'll have to call the Connect method before each call to the Send method. You can use SendTo even after you've established a default remote host with Connect. You can also change the default remote host before calling Send by making another call to Connect.

With a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

Note

You must ensure that the size does not exceed the maximum packet size of the underlying service provider. If it does, the datagram won't be sent and Send 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.

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

Important

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

See also

Applies to

Send(IList<ArraySegment<Byte>>)

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

Sends the set of buffers in the list to a connected Socket.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> -> int
Public Function Send (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 data to be sent.

Returns

The number of bytes sent to the Socket.

Exceptions

buffers is null.

buffers is empty.

An error occurred when attempting to access the socket. See remarks section below.

The Socket has been closed.

Remarks

Send can be used for both connection-oriented and connectionless protocols.

This overload requires at least one buffer that contains the data you want to send.

If you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you will have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

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

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

Sends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags -> int
Public Function Send (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 data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes sent to the Socket.

Exceptions

buffers is null.

buffers is empty.

An error occurred when attempting to access the socket.

The Socket has been closed.

Remarks

This overload requires at least one buffer that contains the data you want to send. The SocketFlags value defaults to 0. If you specify the DontRoute flag as the socketFlags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you will have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In non-blocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

Send(Byte[], SocketFlags)

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

Sends data to a connected Socket using the specified SocketFlags.

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

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

Returns

The number of bytes sent to the Socket.

Exceptions

buffer is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example demonstrates sending 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

Send synchronously sends data to the remote host established in the Connect or Accept method and returns the number of bytes successfully sent. The Send method can be used for both connection-oriented and connectionless protocols.

This overload requires a buffer that contains the data you want to send and a bitwise combination of SocketFlags. The buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer. If you specify the DontRoute flag as the socketflags parameter value, the data you are sending will not be routed.

If you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you will have to call the Connect method before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

Note

You must ensure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and Send 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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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

Send(ReadOnlySpan<Byte>, SocketFlags)

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

Sends data to a connected Socket using the specified SocketFlags.

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

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

socketFlags
SocketFlags

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

Returns

The number of bytes sent to the Socket.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

See also

Applies to

Send(Byte[])

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

Sends data to a connected Socket.

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

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

Returns

The number of bytes sent to the Socket.

Exceptions

buffer is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example demonstrates sending 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

Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send can be used for both connection-oriented and connectionless protocols.

This overload requires a buffer that contains the data you want to send. The SocketFlags value defaults to 0, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.

If you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. If you do not use the SendTo method, you will have to call Connect before each call to Send. You can use SendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling Send by making another call to Connect.

If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. If the time-out value was exceeded, the Send call will throw a SocketException. In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

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 successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

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