Socket.ReceiveMessageFromAsync Metodo

Definizione

Overload

ReceiveMessageFromAsync(SocketAsyncEventArgs)

Avvia la ricezione asincrona del numero di byte specificato in una posizione specificata nel buffer di dati, utilizzando l'oggetto SocketFlags specificato, e memorizza l'endpoint e le informazioni sui pacchetti.

ReceiveMessageFromAsync(ArraySegment<Byte>, EndPoint)

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

ReceiveMessageFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

ReceiveMessageFromAsync(Memory<Byte>, EndPoint, CancellationToken)

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

ReceiveMessageFromAsync(Memory<Byte>, SocketFlags, EndPoint, CancellationToken)

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

ReceiveMessageFromAsync(SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Avvia la ricezione asincrona del numero di byte specificato in una posizione specificata nel buffer di dati, utilizzando l'oggetto SocketFlags specificato, e memorizza l'endpoint e le informazioni sui pacchetti.

public:
 bool ReceiveMessageFromAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool ReceiveMessageFromAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.ReceiveMessageFromAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function ReceiveMessageFromAsync (e As SocketAsyncEventArgs) As Boolean

Parametri

e
SocketAsyncEventArgs

Oggetto SocketAsyncEventArgs da utilizzare per questa operazione socket asincrona.

Restituisce

true se l'operazione di I/O è in sospeso. Al completamento dell'operazione verrà generato l'evento Completed sul parametro e.

false se l'operazione di I/O è stata completata in modo sincrono. In questo caso, l'evento Completed sul parametro e non verrà generato e l'oggetto e passato come parametro potrebbe essere esaminato immediatamente dopo che la chiamata al metodo è stata restituita per recuperare il risultato dell'operazione.

Eccezioni

L'oggetto RemoteEndPoint non può essere null.

L'oggetto Socket è stato chiuso.

Si è verificato un errore durante il tentativo di accesso al socket.

Commenti

Il ReceiveMessageFromAsync metodo viene usato principalmente per ricevere i dati dei messaggi in un socket senza connessione. L'indirizzo locale del socket deve essere noto. Questo metodo può essere usato solo con datagram e socket non elaborati. Il socket deve essere inizializzato con il tipo di socket impostato su Dgram o Raw prima di chiamare questo metodo. Questa operazione può essere eseguita quando il socket viene costruito usando Socket.

Il chiamante deve impostare la SocketAsyncEventArgs.RemoteEndPoint proprietà su uno EndPoint dello stesso tipo dell'endpoint dell'host remoto. La proprietà verrà aggiornata in caso di esito positivo della ricezione all'endpoint remoto effettivo.

Per chiamare correttamente questo metodo, sono necessarie le proprietà e gli eventi seguenti nell'oggetto System.Net.Sockets.SocketAsyncEventArgs :

Il chiamante può impostare la SocketAsyncEventArgs.UserToken proprietà su qualsiasi oggetto stato utente desiderato prima di chiamare il ReceiveMessageFromAsync metodo, in modo che le informazioni saranno recuperabili nel metodo di callback. Se il callback richiede più informazioni di un singolo oggetto, è possibile creare una classe piccola per contenere le altre informazioni sullo stato necessarie come membri.

Per i socket orientati ai messaggi, un messaggio in ingresso viene inserito nel buffer fino alla dimensione totale del buffer. Le SocketAsyncEventArgs.Count proprietà e SocketAsyncEventArgs.Offset determinano dove vengono inseriti i dati nel buffer e la quantità di dati.

Il ReceiveMessageFromAsync metodo imposta automaticamente l'opzione PacketInformation socket alla true prima chiamata per un determinato Socketoggetto . Tuttavia, l'oggetto IPPacketInformation sarà valido solo per i pacchetti che arrivano al computer locale dopo che l'opzione socket è stata impostata. Se un socket viene inviato pacchetti tra quando il socket è associato a un endpoint locale (in modo esplicito dal Bind metodo o in modo implicito da uno dei Connectmetodi , ConnectAsync, SendToo SendToAsync ) e la prima chiamata al metodo, le chiamate al ReceiveMessageFromAsyncReceiveMessageFromAsync metodo generano oggetti non validi IPPacketInformation per questi pacchetti.

Per assicurarsi che tutti gli IPPacketInformation oggetti siano validi, un'applicazione deve impostare l'opzione PacketInformation socket sutrue prima di essere associata a un endpoint locale usando il SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) metodo .

Un'applicazione può esaminare gli oggetti risultanti IPPacketInformation se deve sapere se il datagram è stato inviato usando un indirizzo unicast, multicast o broadcast.

Vedi anche

Si applica a

ReceiveMessageFromAsync(ArraySegment<Byte>, EndPoint)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::SocketReceiveMessageFromResult> ^ ReceiveMessageFromAsync(ArraySegment<System::Byte> buffer, System::Net::EndPoint ^ remoteEndPoint);
public System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveMessageFromResult> ReceiveMessageFromAsync (ArraySegment<byte> buffer, System.Net.EndPoint remoteEndPoint);
member this.ReceiveMessageFromAsync : ArraySegment<byte> * System.Net.EndPoint -> System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveMessageFromResult>
Public Function ReceiveMessageFromAsync (buffer As ArraySegment(Of Byte), remoteEndPoint As EndPoint) As Task(Of SocketReceiveMessageFromResult)

Parametri

buffer
ArraySegment<Byte>

Buffer per i dati ricevuti.

remoteEndPoint
EndPoint

Endpoint dello stesso tipo dell'endpoint dell'host remoto.

Restituisce

Attività asincrona completa con un SocketReceiveMessageFromResult contenente il numero di byte ricevuti e informazioni aggiuntive sull'host di invio.

Eccezioni

L'oggetto Socket è stato chiuso.

remoteEndPoint è null.

È necessario chiamare il metodo Bind prima di eseguire questa operazione.

Si applica a

ReceiveMessageFromAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

public:
 System::Threading::Tasks::Task<System::Net::Sockets::SocketReceiveMessageFromResult> ^ ReceiveMessageFromAsync(ArraySegment<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEndPoint);
public System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveMessageFromResult> ReceiveMessageFromAsync (ArraySegment<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEndPoint);
member this.ReceiveMessageFromAsync : ArraySegment<byte> * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> System.Threading.Tasks.Task<System.Net.Sockets.SocketReceiveMessageFromResult>
Public Function ReceiveMessageFromAsync (buffer As ArraySegment(Of Byte), socketFlags As SocketFlags, remoteEndPoint As EndPoint) As Task(Of SocketReceiveMessageFromResult)

Parametri

buffer
ArraySegment<Byte>

Buffer per i dati ricevuti.

socketFlags
SocketFlags

Combinazione bit per bit dei valori SocketFlags che verranno usati durante la ricezione dei dati.

remoteEndPoint
EndPoint

Endpoint dello stesso tipo dell'endpoint dell'host remoto.

Restituisce

Attività asincrona completa con un SocketReceiveMessageFromResult contenente il numero di byte ricevuti e informazioni aggiuntive sull'host di invio.

Eccezioni

L'oggetto Socket è stato chiuso.

remoteEndPoint è null.

È necessario chiamare il metodo Bind prima di eseguire questa operazione.

Si applica a

ReceiveMessageFromAsync(Memory<Byte>, EndPoint, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

public System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveMessageFromResult> ReceiveMessageFromAsync (Memory<byte> buffer, System.Net.EndPoint remoteEndPoint, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveMessageFromAsync : Memory<byte> * System.Net.EndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveMessageFromResult>
Public Function ReceiveMessageFromAsync (buffer As Memory(Of Byte), remoteEndPoint As EndPoint, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of SocketReceiveMessageFromResult)

Parametri

buffer
Memory<Byte>

Buffer per i dati ricevuti.

remoteEndPoint
EndPoint

Endpoint dello stesso tipo dell'endpoint dell'host remoto.

cancellationToken
CancellationToken

Token di annullamento che può essere usato per segnalare che l'operazione asincrona deve essere annullata.

Restituisce

Attività asincrona completa con un SocketReceiveMessageFromResult contenente il numero di byte ricevuti e informazioni aggiuntive sull'host di invio.

Eccezioni

L'oggetto Socket è stato chiuso.

remoteEndPoint è null.

È necessario chiamare il metodo Bind prima di eseguire questa operazione.

Il token di annullamento è stato annullato. Questa eccezione viene archiviata nell'attività restituita.

Si applica a

ReceiveMessageFromAsync(Memory<Byte>, SocketFlags, EndPoint, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Riceve i dati e restituisce informazioni aggiuntive sul mittente del messaggio.

public System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveMessageFromResult> ReceiveMessageFromAsync (Memory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEndPoint, System.Threading.CancellationToken cancellationToken = default);
member this.ReceiveMessageFromAsync : Memory<byte> * System.Net.Sockets.SocketFlags * System.Net.EndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<System.Net.Sockets.SocketReceiveMessageFromResult>
Public Function ReceiveMessageFromAsync (buffer As Memory(Of Byte), socketFlags As SocketFlags, remoteEndPoint As EndPoint, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of SocketReceiveMessageFromResult)

Parametri

buffer
Memory<Byte>

Buffer per i dati ricevuti.

socketFlags
SocketFlags

Combinazione bit per bit dei valori SocketFlags che verranno usati durante la ricezione dei dati.

remoteEndPoint
EndPoint

Endpoint dello stesso tipo dell'endpoint dell'host remoto.

cancellationToken
CancellationToken

Token di annullamento che può essere usato per segnalare che l'operazione asincrona deve essere annullata.

Restituisce

Attività asincrona completa con un SocketReceiveMessageFromResult contenente il numero di byte ricevuti e informazioni aggiuntive sull'host di invio.

Eccezioni

L'oggetto Socket è stato chiuso.

remoteEndPoint è null.

È necessario chiamare il metodo Bind prima di eseguire questa operazione.

Il token di annullamento è stato annullato. Questa eccezione viene archiviata nell'attività restituita.

Si applica a