Namespace: System.Net.Sockets
Sends the specified number of bytes of data to a connected Socket using the specified buffer.
This method is typically used for connection-oriented protocols like TCP. It attempts to send all the data in the buffer.
If the socket is connection-oriented and has not been connected, a SocketException is thrown.
public static int Send(
Socket socket,
byte[] buffer
);
buffer
(byte[]): A byte array containing the data to send.
intThe number of bytes sent to the Socket. This value can be less than the number of bytes requested to be sent. For connectionless sockets, this value is the number of bytes actually sent to the datagram. For connection-oriented sockets, this value is the number of bytes actually sent to the stream. If an error occurs, an exception is thrown.
SocketException: An error occurred during the send operation.ArgumentNullException: The socket or buffer parameter is null.ObjectDisposedException: The Socket has been closed.
When you use the Send method on a connection-oriented socket, it attempts to send the entire contents of the buffer. If the socket is a blocking socket, this method will block until all data is sent or an error occurs. If the socket is non-blocking and there is not enough space in the send buffer to accommodate the entire buffer, Send will send as much data as it can and return the number of bytes sent.
For connectionless sockets, the Send method sends a single datagram. If the datagram is too large to fit into the buffer, the datagram is truncated and sent.
If you are using the Send method on a connection-oriented socket and need to ensure that all data is sent, you should loop, calling Send until all data has been sent.
You can use the SendTo method to send data to a specific remote host when using a connectionless socket.
The following example demonstrates how to send data to a connected Socket.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketSender
{
public static void SendData(Socket clientSocket)
{
try
{
string message = "Hello, server!";
byte[] data = Encoding.UTF8.GetBytes(message);
int bytesSent = Socket.Send(clientSocket, data);
Console.WriteLine($"Sent {bytesSent} bytes: {message}");
// For connection-oriented sockets, you might need to send in a loop
// if you want to ensure all data is sent.
int totalBytesSent = 0;
while (totalBytesSent < data.Length)
{
bytesSent = Socket.Send(clientSocket, data, totalBytesSent, data.Length - totalBytesSent, SocketFlags.None);
if (bytesSent == 0)
{
// Connection closed by remote host
break;
}
totalBytesSent += bytesSent;
}
Console.WriteLine($"Ensured all {totalBytesSent} bytes were sent.");
}
catch (SocketException ex)
{
Console.WriteLine($"Socket error occurred: {ex.Message}");
}
catch (ArgumentNullException ex)
{
Console.WriteLine($"Argument null error: {ex.Message}");
}
catch (ObjectDisposedException ex)
{
Console.WriteLine($"Socket disposed error: {ex.Message}");
}
}
// Example usage (assuming you have a connected clientSocket)
// public static void Main(string[] args)
// {
// // ... setup and connect clientSocket ...
// // Socket connectedClient = ...
// // SendData(connectedClient);
// // ... close socket ...
// }
}
Socket TypeRepresents a network socket.
SocketException ClassRepresents an error returned by a Windows Sockets API call.
ArgumentNullException ClassThrown when a null argument is passed to a method that does not accept it.
ObjectDisposedException ClassThrown when an operation is performed on a disposed object.