Επεξεργασία

Κοινή χρήση μέσω


NetworkStream.Read Method

Definition

Overloads

Read(Span<Byte>)

Reads data from the NetworkStream and stores it to a span of bytes in memory.

Read(Byte[], Int32, Int32)

Reads data from the NetworkStream and stores it to a byte array.

Read(Span<Byte>)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

Reads data from the NetworkStream and stores it to a span of bytes in memory.

public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

Parameters

buffer
Span<Byte>

A region of memory to store data read from the NetworkStream.

Returns

The number of bytes read from the NetworkStream.

Exceptions

The NetworkStream does not support reading.

An error occurred when accessing the socket.

-or-

There is a failure reading from the network.

Remarks

This method reads as much data as is available into the buffer parameter and returns the number of bytes successfully read.

Note

Check to see if the NetworkStream is readable by calling the CanRead property. If you attempt to read from a NetworkStream that is not readable, you will get an InvalidOperationException.

Note

If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to

Read(Byte[], Int32, Int32)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

Reads data from the NetworkStream and stores it to a byte array.

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int size);
public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int size);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, size As Integer) As Integer
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

Parameters

buffer
Byte[]

An array of type Byte that is the location in memory to store data read from the NetworkStream.

offset
Int32

The location in buffer to begin storing the data to.

sizecount
Int32

The number of bytes to read from the NetworkStream.

Returns

The number of bytes read from the NetworkStream.

Exceptions

buffer is null.

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 offset.

The NetworkStream does not support reading.

An error occurred when accessing the socket.

-or-

There is a failure reading from the network.

Examples

The following code example uses DataAvailable to determine if data is available to be read. If data is available, it reads from the NetworkStream.

byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

// Read all the data until the end of stream has been reached.
// The incoming message may be larger than the buffer size.
while (numberOfBytesRead > 0)
{
    myCompleteMessage.Append(Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
}

// Print out the received message to the console.
Console.WriteLine("You received the following message : " + myCompleteMessage);

Remarks

This method reads data into buffer and returns the number of bytes successfully read. The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter.

Note

Check to see if the NetworkStream is readable by calling the CanRead property. If you attempt to read from a NetworkStream that is not readable, you will get an InvalidOperationException.

Note

If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to