Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Begins an asynchronous write operation that writes Bytes from the specified buffer to the stream.
public:
override IAsyncResult ^ BeginWrite(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState);
override this.BeginWrite : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginWrite (buffer As Byte(), offset As Integer, count As Integer, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult
The zero-based location in buffer
at which to begin reading bytes to be written to the stream.
An AsyncCallback delegate that references the method to invoke when the write operation is complete.
A user-defined object that contains information about the write operation. This object is passed to the asyncCallback
delegate when the operation completes.
An IAsyncResult object indicating the status of the asynchronous operation.
buffer
is null
.
offset
is less than zero.
-or-
offset
is greater than the length of buffer
.
-or-
offset
+ count is greater than the length of buffer
.
The write operation failed.
There is already a write operation in progress.
This object has been closed.
Authentication has not occurred.
The following code example demonstrates calling this method.
void ReadCallback(IAsyncResult ar)
{
ClientState state = (ClientState) ar.AsyncState;
SslStream stream = state.stream;
// Read the message sent by the client.
// The end of the message is signaled using the
// "<EOF>" marker.
int byteCount = -1;
try
{
Console.WriteLine("Reading data from the client.");
byteCount = stream.EndRead(ar);
// Use Decoder class to convert from bytes to UTF8
// in case a character spans two buffers.
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(state.buffer,0, byteCount)];
decoder.GetChars(state.buffer, 0, byteCount, chars,0);
state.readData.Append (chars);
// Check for EOF or an empty message.
if (state.readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
{
// We are not finished reading.
// Asynchronously read more message data from the client.
stream.BeginRead(state.buffer, 0, state.buffer.Length,
new AsyncCallback(ReadCallback),
state);
}
else
{
Console.WriteLine("Message from the client: {0}", state.readData.ToString());
}
// Encode a test message into a byte array.
// Signal the end of the message using "<EOF>".
byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
// Asynchronously send the message to the client.
stream.BeginWrite(message, 0, message.Length,
new AsyncCallback(WriteCallback),
state);
}
catch (Exception readException)
{
Console.WriteLine("Read error: {0}", readException.Message);
state.Close();
return;
}
}
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in