本文整理匯總了VB.NET中System.Net.Sockets.UdpClient類的典型用法代碼示例。如果您正苦於以下問題:VB.NET UdpClient類的具體用法?VB.NET UdpClient怎麽用?VB.NET UdpClient使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了UdpClient類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: UdpClient
' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient(11000)
Try
udpClient.Connect("www.contoso.com", 11000)
' Sends a message to the host to which you have connected.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
udpClient.Send(sendBytes, sendBytes.Length)
' Sends message to a different host using optional hostname and port parameters.
Dim udpClientB As New UdpClient()
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
' IPEndPoint object will allow us to read datagrams sent from any source.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
' UdpClient.Receive blocks until a message is received from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
' Which one of these two hosts responded?
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()))
udpClient.Close()
udpClientB.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub