3

我有一个代码

*已编辑,带有整个代码片段

 Dim receivingUdpClient As New UdpClient(20000)

   Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
   Try 

      Console.WriteLine("listening")

      Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)

      Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)

      Console.WriteLine(receiveBytes)

   Catch e As Exception
      Console.WriteLine(e.ToString())
   End Try 

但它给了我一个错误。

在 system.dll 中发生了 system.net.sockets.socketexception' 类型的第一次机会异常

我真的很困惑这意味着什么。

4

2 回答 2

2

试试下面的代码

Dim receivingUdpClient As New UdpClient(20000)
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes As [Byte]()
Dim returnData As String

while ("Your condition")

Try 

  Console.WriteLine("listening")

  receiveBytes = receivingUdpClient.Receive(RemoteIpEndPoint)

  returnData = Encoding.ASCII.GetString(receiveBytes)

  Console.WriteLine(receiveBytes)

Catch e As Exception
  Console.WriteLine(e.ToString())
End Try 

End While
于 2012-09-13T14:06:10.343 回答
0

调试应用程序时,只要遇到异常,调试器就会收到通知。此时,应用程序被挂起,调试器决定如何处理异常。第一次通过这种机制称为“第一次机会”异常。

首次机会异常消息通常并不意味着代码中存在问题。对于优雅地处理异常的应用程序/组件,第一次机会异常消息让开发人员知道遇到并处理了异常情况。

对于没有异常处理的代码,调试器将收到第二次机会异常通知,并会因未处理的异常而停止。

检查此链接: Handle Socket.ReceiveFrom with timeout without spaming console

对于您的情况,在循环中声明 updClient 将导致以下错误。

Only one usage of each socket address (protocol/network address/port) is normally permitted

您可以查看下面的链接以获取解决方案。或者尝试在循环之外声明它,看看它是否有效。

http://blogs.msdn.com/b/dgorti/archive/2005/09/18/470766.aspx

于 2012-09-13T10:27:02.777 回答