語言

SslStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) 方法

定義

開始一個非同步寫入操作,從指定的緩衝區寫 Byte入資料到串流。

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

參數

buffer
Byte[]

一個 Byte 陣列,提供要寫入串流的位元組。

offset
Int32

以零為 buffer 基礎的位置,開始讀取要寫入資料串流的位元組。

count
Int32

一個 Int32 指定從 讀取 buffer的位元組數的值。

asyncCallback
AsyncCallback

一個 AsyncCallback 代理,當寫入操作完成時會參考要呼叫的方法。

asyncState
Object

一個由使用者定義的物件,包含關於寫入操作的資訊。 當操作完成時,這個物件會傳給代理。asyncCallback

傳回

一個 IAsyncResult 指示非同步操作狀態的物件。

例外狀況

buffer 是 null。

offset 小於零。

-或-

offset 大於 的 buffer長度。

-或-

offset + 計數大於 的 buffer長度。

寫入操作失敗了。

已經有一個寫入操作正在進行中。

這個物件已經關閉了。

尚未進行認證。

範例

下列程式代碼範例示範呼叫這個方法。

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;
    }
}

適用於