3

i have a code

*edited, with the whole code snippet

 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 

but it gives me an error.

a first chance exception of type system.net.sockets.socketexception' occurred in system.dll

i'm really confuse what this means.

fwoop
  • 75
  • 1
  • 3
  • 7

2 Answers2

2

Try the following code

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
Harsh
  • 3,683
  • 2
  • 25
  • 41
  • hi, thanks for the reply. i tried your code but my program stucks in receiveBytes = receivingUdpClient.Receive(RemoteIpEndPoint), the program doesnt stop "loading" – fwoop Sep 13 '12 at 14:41
0

When an application is being debugged, the debugger gets notified whenever an exception is encountered At this point, the application is suspended and the debugger decides how to handle the exception. The first pass through this mechanism is called a "first chance" exception.

First chance exception messages most often do not mean there is a problem in the code. For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.

For code without exception handling, the debugger will receive a second chance exception notification and will stop with a unhandled exception.

Check this link: Handle Socket.ReceiveFrom with timeout without spamming console

For your situation, declaring updClient inside a loop will cause the following error.

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

You can check the link below for the solution. Or try to declare it outside the loop and see if it works.

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

Community
  • 1
  • 1
Harsh
  • 3,683
  • 2
  • 25
  • 41
  • hmm so i tried printing the exception and it gave me a first chance exception of type system.componentmodel.invalidenumargumentexception – fwoop Sep 13 '12 at 10:44
  • Does the flow of your program stops or keeps on running? – Harsh Sep 13 '12 at 10:49
  • actually i have a loop and Dim receivingUdpClient As New UdpClient(20000) is inside the loop, and yes it keeps running. – fwoop Sep 13 '12 at 10:55
  • So, you don't need to worry about this exception as this is already handled. If you want something else then please add the code of the loop as well, that might help. – Harsh Sep 13 '12 at 10:57
  • i edited my question above. so thats the code, and when i debug, im stuck at Dim receivingUdpClient As New UdpClient(20000) – fwoop Sep 13 '12 at 11:03
  • Please check the updated answer. And also check whether declaring UdpClient outside the loop helps. – Harsh Sep 13 '12 at 11:17
  • i tried declaring it outside the loop. still the same, debug throws me at exception at Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint) – fwoop Sep 13 '12 at 11:34
  • You can try declaring it outside the loop as well. – Harsh Sep 13 '12 at 11:43
  • yeap, both of them is already outside of the loop. thanks for the replies btw, im still new to this – fwoop Sep 13 '12 at 11:44
  • You are welcome. Glad that it helped. Please mark it answered, if you got what you were looking for. – Harsh Sep 13 '12 at 11:47
  • i'm sorry but it still didnt fix the problem :( – fwoop Sep 13 '12 at 12:19
  • Can you please elaborate the situation you are facing? or any particular error you are receiving. – Harsh Sep 13 '12 at 12:20
  • theres no actual "error" but when i reach Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint) in debug i get "a first chance exception of type system.net.sockets.socketexception' occurred in system.dll" then the code jumps to catch exception, which Dim returnData As String = Encoding.ASCII.GetString(receiveBytes) Console.WriteLine(receiveBytes) is skipped – fwoop Sep 13 '12 at 13:12