Socket.Receive Method (Byte[], SocketFlags)
Assembly: System (in system.dll)
Parameters
- buffer
An array of type Byte that is the storage location for the received data.
- socketFlags
A bitwise combination of the SocketFlags values.
Return Value
The number of bytes received.The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. You can call Receive from both connection-oriented and connectionless sockets.
This overload only requires you to provide a receive buffer and the necessary SocketFlags. The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.
If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host.
If no data is available for reading, the Receive method will block until data is available. If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry your receive operation.
If you are using a connection-oriented Socket, the Receive method will read as much data as is available up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.
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 |
|---|
| This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing. |
The following code example specifies a data buffer, and SocketFlags for receiving 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.
public static int SendReceiveTest2(Socket server)
{
ubyte msg[] = Encoding.get_UTF8().GetBytes("This is a test");
ubyte bytes[] = new ubyte[256];
try {
// Blocks until send returns.
int i = server.Send(msg, SocketFlags.None);
Console.WriteLine("Sent {0} bytes.", (Int32)i);
// Get reply from the server.
i = server.Receive(bytes, SocketFlags.None);
Console.WriteLine(Encoding.get_UTF8().GetString(bytes));
}
catch (SocketException e) {
Console.WriteLine("{0} Error code: {1}.", e.get_Message(),
(Int32)e.get_ErrorCode());
return e.get_ErrorCode();
}
return 0;
} //SendReceiveTest2
- SocketPermission for accepting connections from the network. Associated enumeration: Accept.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note