ReceiveScheduled Method
public int ReceiveScheduled(
byte[] buffer,
int offset,
int size,
System.Net.Sockets.SocketFlags socketFlags
)
Description
Receives data from a connected Socket and stores the data in the specified buffer, using the specified SocketFlags. This method is used to receive data that has been scheduled for delivery.
The ReceiveScheduled method is an asynchronous operation that is scheduled to complete at a later time. This can be useful in scenarios where you want to avoid blocking the calling thread while waiting for data, or when you need to manage the timing of data reception more precisely.
Parameters
buffer: A byte array that receives the data.offset: The zero-based position in the buffer to begin storing the received data.size: The maximum number of bytes to receive.socketFlags: A bitwise combination of the enumeration values that specify how the receive operation should be performed.
Return Value
The number of bytes received. This will be less than or equal to the number of bytes requested. Returns 0 if the remote host has shut down and all data has been received.
Remarks
Use the ReceiveScheduled method to receive data from a Socket. This method is suitable for receiving data that has been explicitly scheduled for delivery, offering more control over the reception timing compared to regular Receive operations.
The method will block until data is available or until the socket is closed.
If you are using the Socket class in an application that must be highly responsive, use the asynchronous methods such as ReceiveAsync.
Exceptions
SocketException: An error occurred when accessing the socket.ObjectDisposedException: The Socket has been closed.ArgumentNullException: Thebufferis null.ArgumentOutOfRangeException: Theoffsetorsizeparameters are out of range.
Example
The following example demonstrates how to use the ReceiveScheduled method to receive data from a connected socket.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketExample
{
public static void Main(string[] args)
{
// Assumes a connected socket 'connectedSocket' is available.
// This is a simplified example and doesn't cover socket creation and connection.
Socket connectedSocket = null; // Placeholder for a connected socket
try
{
// Example setup for a connected socket (replace with actual connection logic)
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
connectedSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// In a real scenario, you would call connectedSocket.Connect(remoteEP);
Console.WriteLine("Simulating a connected socket.");
byte[] buffer = new byte[1024];
int bytesRead = 0;
int offset = 0;
int size = buffer.Length;
SocketFlags flags = SocketFlags.None;
Console.WriteLine("Attempting to receive scheduled data...");
// In a real application, you would ensure the socket is connected before calling ReceiveScheduled.
// For demonstration, we'll simulate receiving data.
// connectedSocket.Connect(remoteEP); // Uncomment in a real scenario
// For demonstration purposes, let's assume some data is available.
// In a real scenario, the actual data would come from the network.
// Simulating receiving data
string simulatedData = "Hello from the server!";
byte[] simulatedBytes = Encoding.ASCII.GetBytes(simulatedData);
// This part would be handled by the OS/network stack if data was truly received.
// For this example, we'll bypass the actual network receive and just simulate bytesRead.
// Simulate a successful receive. In a real async scenario, this might be in a callback.
// For this synchronous-like example (ReceiveScheduled is blocking), we'll pretend data arrived.
if (simulatedBytes.Length > 0)
{
int bytesToCopy = Math.Min(simulatedBytes.Length, size);
Array.Copy(simulatedBytes, 0, buffer, offset, bytesToCopy);
bytesRead = bytesToCopy;
}
else
{
bytesRead = 0; // No data received
}
if (bytesRead > 0)
{
string receivedData = Encoding.ASCII.GetString(buffer, offset, bytesRead);
Console.WriteLine($"Received {bytesRead} bytes: {receivedData}");
}
else
{
Console.WriteLine("No data received.");
}
}
catch (SocketException se)
{
Console.WriteLine($"SocketException: {se.Message}");
}
catch (Exception e)
{
Console.WriteLine($"General Exception: {e.Message}");
}
finally
{
if (connectedSocket != null && connectedSocket.Connected)
{
connectedSocket.Shutdown(SocketShutdown.Both);
connectedSocket.Close();
Console.WriteLine("Socket closed.");
}
}
}
}