SslStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Avvia un'operazione di scrittura asincrona che scrive Bytes dal buffer specificato al flusso.
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
Parametri
- offset
- Int32
Posizione in base zero in in buffer cui iniziare la lettura dei byte da scrivere nel flusso.
- asyncCallback
- AsyncCallback
Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione di scrittura.
- asyncState
- Object
Oggetto definito dall'utente che contiene informazioni sull'operazione di scrittura. Questo oggetto viene passato al asyncCallback delegato al termine dell'operazione.
Valori restituiti
Oggetto IAsyncResult che indica lo stato dell'operazione asincrona.
Eccezioni
buffer è null.
offset è minore di zero.
oppure
offset è maggiore della lunghezza di buffer.
oppure
offset + count è maggiore della lunghezza di buffer.
Operazione di scrittura non riuscita.
È già in corso un'operazione di scrittura.
Questo oggetto è stato chiuso.
L'autenticazione non è stata eseguita.
Esempio
Nell'esempio di codice seguente viene illustrata la chiamata a questo metodo.
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;
}
}