UdpClient.Receive(IPEndPoint) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve um datagrama UDP enviado por um host remoto.
public:
cli::array <System::Byte> ^ Receive(System::Net::IPEndPoint ^ % remoteEP);
public byte[] Receive(ref System.Net.IPEndPoint remoteEP);
member this.Receive : IPEndPoint -> byte[]
Public Function Receive (ByRef remoteEP As IPEndPoint) As Byte()
Parâmetros
- remoteEP
- IPEndPoint
E IPEndPoint que representa o host remoto de onde os dados foram enviados.
Devoluções
Um array de tipos Byte que contém dados de datagramas.
Exceções
O subjacente Socket foi encerrado.
Ocorreu um erro ao aceder ao soquete.
Exemplos
O exemplo seguinte demonstra o Receive método. O Receive método bloqueia a execução até receber uma mensagem. Usando o IPEndPoint passado para Receive, revela-se a identidade do hospedeiro responsivo.
//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(11000);
//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
try{
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
}
catch ( Exception e ){
Console.WriteLine(e.ToString());
}
'Creates a UdpClient for reading incoming data.
Dim receivingUdpClient As New UdpClient(11000)
'Creates an IPEndPoint to record the IP address and port number of the sender.
' The IPEndPoint will allow you to read datagrams sent from any source.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Try
' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
Console.WriteLine(("This is the message you received " + returnData.ToString()))
Console.WriteLine(("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()))
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
Observações
O Receive método irá bloquear até que um datagrama chegue de um host remoto. Quando os dados estão disponíveis, o Receive método lê o primeiro datagrama enfileirado e devolve a parte dos dados como um array de bytes. Este método preenche o remoteEP parâmetro com o IPAddress número de porta e do remetente.
Se especificar um host remoto por defeito no Connect método, este Receive aceitará datagramas apenas desse host. Todos os outros datagramas serão descartados.
Se receber um SocketException, use SocketException.ErrorCode para obter o código de erro específico. Depois de obter este código, pode consultar a documentação do código de erro da API
Note
Se pretende receber datagramas multicast, não chame o Connect método antes de o Receive chamar. O UdpClient que usa para receber datagramas deve ser criado usando o número da porta multicast.