Export (0) Print
Expand All

NetworkStream.BeginRead Method

Begins an asynchronous read from the NetworkStream.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public override IAsyncResult BeginRead(
	byte[] buffer,
	int offset,
	int size,
	AsyncCallback callback,
	Object state
)

Parameters

buffer
Type: System.Byte[]

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

offset
Type: System.Int32

The location in buffer to begin storing the data.

size
Type: System.Int32

The number of bytes to read from the NetworkStream.

callback
Type: System.AsyncCallback

The AsyncCallback delegate that is executed when BeginRead completes.

state
Type: System.Object

An object that contains any additional user-defined data.

Return Value

Type: System.IAsyncResult
An IAsyncResult that represents the asynchronous call.

ExceptionCondition
ArgumentNullException

The buffer parameter is null.

ArgumentOutOfRangeException

The offset parameter is less than 0.

-or-

The offset parameter is greater than the length of the buffer paramater.

-or-

The size is less than 0.

-or-

The size is greater than the length of buffer minus the value of the offset parameter.

IOException

The underlying Socket is closed.

-or-

There was a failure while reading from the network.

-or-

An error occurred when accessing the socket. See the Remarks section for more information.

ObjectDisposedException

The NetworkStream is closed.

The BeginRead method starts asynchronously reading data from the incoming network buffers. Calling the BeginRead method gives you the ability to receive data within a separate execution thread.

You must create a callback method that implements the AsyncCallback delegate and pass its name to the BeginRead method. At the very minimum, your state parameter must contain the NetworkStream. Because you will want to obtain the received data within your callback method, you should create a small class or structure to hold a read buffer and any other useful information. Pass the structure or class instance to the BeginRead method through the state parameter.

Your callback method should call the EndRead method. When your application calls BeginRead, the system will wait until data is received or an error occurs, and then the system will use a separate thread to execute the specified callback method, and blocks on EndRead until the provided NetworkStream reads data or throws an exception. If you want the original thread to block after you call the BeginRead method, use the WaitOne method. Call Set in the callback method when you want the original thread to continue executing. For additional information about writing callback methods, see Marshaling a Delegate as a Callback Method.

The BeginRead method reads as much data as is available, up to the number of bytes specified by the size parameter.

NoteNote

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 in MSDN for a detailed description of the error.

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example uses BeginRead to read data asynchronously from the network stream. The myReadCallBack method implements the AsyncCallback delegate and is called by the system when BeginRead returns.

            // Example of CanRead, and BeginRead. 

            // Check to see if this NetworkStream is readable. 
            if(myNetworkStream.CanRead){
            	
                byte[] myReadBuffer = new byte[1024];
                myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, 
                                                             new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack), 
                                                             myNetworkStream);  

                allDone.WaitOne();
            }
            else{
                 Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
            }

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft