SslStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Commence une opération d’écriture asynchrone qui écrit Bytedes s à partir de la mémoire tampon spécifiée dans le flux.
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);
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
Paramètres
- offset
- Int32
Emplacement de base zéro dans buffer lequel commencer la lecture d’octets à écrire dans le flux.
- asyncCallback
- AsyncCallback
Délégué AsyncCallback qui fait référence à la méthode à appeler lorsque l’opération d’écriture est terminée.
- asyncState
- Object
Objet défini par l’utilisateur qui contient des informations sur l’opération d’écriture. Cet objet est transmis au asyncCallback délégué une fois l’opération terminée.
Retours
Objet IAsyncResult indiquant l’état de l’opération asynchrone.
Exceptions
buffer a la valeur null.
offset est inférieur à zéro.
-ou-
offset est supérieur à la longueur de buffer.
-ou-
offset + nombre est supérieur à la longueur de buffer.
Échec de l’opération d’écriture.
Il existe déjà une opération d’écriture en cours.
Cet objet a été fermé.
L’authentification n’a pas eu lieu.
Exemples
L’exemple de code suivant illustre l’appel de cette méthode.
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;
}
}