System.Net.Sockets.Socket.Send Method

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

Syntax

public int Send (byte[] buf, int offset, int size, SocketFlags flags)

Parameters

buf
Documentation for this section has not yet been entered.
flags
Documentation for this section has not yet been entered.
offset
The position in the data buffer at which to begin sending data.
size
The number of bytes to send.

Returns

The number of bytes sent to the System.Net.Sockets.Socket.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

InvalidOperationExceptionAn asynchronous call is pending and a blocking method has been called.
System.Net.Sockets.SocketException

socketFlags is not a valid combination of values.

-or-

An error occurred while accessing the socket.

Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

ObjectDisposedExceptionThe current instance has been disposed.

Remarks

Socket.Send(Byte[], int, SocketFlags) synchronously sends data to the remote host specified in the Socket.Connect(System.Net.EndPoint) or Socket.Accept method and returns the number of bytes successfully sent. Socket.Send(Byte[], int, SocketFlags) can be used for both connection-oriented and connectionless protocols.

In this overload, if you specify the SocketFlags.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 Socket.Connect(System.Net.EndPoint) before calling this method or Socket.Send(Byte[], int, SocketFlags) will throw a System.Net.Sockets.SocketException. If you are using a connection-oriented protocol, you must either use Socket.Connect(System.Net.EndPoint) to establish a remote host connection, or use Socket.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 Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint). If you do not use Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint), you will have to call Socket.Connect(System.Net.EndPoint) before each call to Socket.Send(Byte[], int, SocketFlags). It is okay to use Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) even after you have established a default remote host with Socket.Connect(System.Net.EndPoint). You can also change the default remote host prior to calling Socket.Send(Byte[], int, SocketFlags) by making another call to Socket.Connect(System.Net.EndPoint).

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 Socket.Send(Byte[], int, SocketFlags) will throw a System.Net.Sockets.SocketException.

If you are using a connection-oriented protocol, Socket.Send(Byte[], int, SocketFlags) 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 Socket.Send(Byte[], int, SocketFlags) call will throw a System.Net.Sockets.SocketException. In nonblocking mode, Socket.Send(Byte[], int, SocketFlags) 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 Socket.Send(Byte[], int, SocketFlags) method means that the underlying system has had room to buffer your data for a network send.

Note:

If you receive a System.Net.Sockets.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.

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 [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0