Socket.Send Method (Byte[], SocketFlags)

Sends data to a connected Socket using the specified SocketFlags.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public int Send(
	byte[] buffer,
	SocketFlags socketFlags
)

Parameters

buffer
Type: System.Byte[]

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

socketFlags
Type: System.Net.Sockets.SocketFlags

A bitwise combination of the SocketFlags values.

Return Value

Type: System.Int32
The number of bytes sent to the Socket.

ExceptionCondition
ArgumentNullException

buffer is null.

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

ObjectDisposedException

The Socket has been closed.

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.

NoteNote:

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

NoteNote:

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.

NoteNote:

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

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The consequence of sending zero-length data with this method is determined by the native operating system and not by the .NET Compact Framework.

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. 
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.
int SendReceiveTest2(Socket* server)
{
    Byte msg[]= Encoding::UTF8->GetBytes(S"This is a test");
    Byte bytes[] = new Byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server->Send(msg, SocketFlags::None);
        Console::WriteLine(S"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(S"{0} Error code: {1}.", e->Message, e->ErrorCode.ToString());
        return (e->ErrorCode);
    }
    return 0;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft