本文整理汇总了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