Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.
Assembly: System (in System.dll)
Public Function Send ( _ buffer As Byte(), _ offset As Integer, _ size As Integer, _ socketFlags As SocketFlags _ ) As Integer
public int Send( byte[] buffer, int offset, int size, SocketFlags socketFlags )
public: int Send( array<unsigned char>^ buffer, int offset, int size, SocketFlags socketFlags )
member Send : buffer:byte[] * offset:int * size:int * socketFlags:SocketFlags -> int
Parameters
- buffer
- Type: System.Byte[]
An array of type Byte that contains the data to be sent.
- offset
- Type: System.Int32
The position in the data buffer at which to begin sending data.
- size
- Type: System.Int32
The number of bytes to send.
- socketFlags
- Type: System.Net.Sockets.SocketFlags
A bitwise combination of the SocketFlags values.
| Exception | Condition |
|---|---|
| ArgumentNullException |
buffer is null. |
| ArgumentOutOfRangeException |
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. |
| SocketException |
socketFlags is not a valid combination of values. -or- An operating system error occurs while accessing the Socket. See the Remarks section for more information. |
| ObjectDisposedException |
The Socket has been closed. |
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 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 Network Tracing. |
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. 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
// 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, 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); return (e.ErrorCode); } return 0; }
// 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; }
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note