Wednesday, October 5, 2016

Already running application now gets socket error 10013

Leave a Comment

I have an application done in VB.NET that listen on a specific UDP port and answer through the same port to the IP that send the packet. It was working ok from a couple of years to the last month; now when try to answer crash due to socket error 10013.

I even try an older version that I know it was working too and get the same crash.

I try disabling Microsoft Security Essentials real time protection and Windows firewall and didn't work.

In the code I have the line

MyUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True) 

I have no clue about what to do, I'm lost.

Any idea how to solve this?

Edit: Here's the code

#Region "UDP Send variables"    Dim GLOIP As IPAddress    Dim GLOINTPORT As Integer    Dim bytCommand As Byte() = New Byte() {} #End Region  Dim MyUdpClient As New UdpClient()  Private Sub StartUdpBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartUdpBtn.Click       If StartUdpBtn.Tag = 0 Then           '   If Not UdpOpen Then           StartUdpReceiveThread(CInt(ListeningPortLbl.Text))           'End If       Else           If ThreadReceive.IsAlive Then               ThreadReceive.Abort()               MyUdpClient.Close()               PrintLog("UDP port closed")               StartUdpBtn.Tag = 0               UdpOpen = False               StartUdpBtn.Text = "Start UDP"           End If       End If        If UdpOpen Then           StartUdpBtn.Tag = 1           StartUdpBtn.Text = "Stop UDP"       Else           StartUdpBtn.Tag = 0           StartUdpBtn.Text = "Start UDP"           TimerUDP.Enabled = False           TiempoUDP.Stop()           TiempoUdpLbl.Text = "--:--:--"       End If    End Sub  Private Sub StartUdpReceiveThread(ByVal Port As Integer)     Dim UdpAlreadyOpen As Boolean = False     Try         If Not UdpOpen Then             MyUdpClient = New UdpClient(Port)             MyUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)             UdpAlreadyOpen = True         Else             Me.Invoke(Sub()                           TiempoUDP.Restart()                           If TimerUDP.Enabled = False Then                               TimerUDP.Enabled = True                           End If                       End Sub)         End If          ThreadReceive = New System.Threading.Thread(AddressOf UdpReceive)         ThreadReceive.IsBackground = True          ThreadReceive.Start()         UdpOpen = True          If UdpAlreadyOpen Then                  PrintLog(String.Format("UDP port {0} opened, waiting data...", Port.ToString))         End If     Catch ex As Exception         PrintErrorLog(ex.Message)         PrintErrorLog(ex.StackTrace)     End Try End Sub  Private Sub UdpReceive()     Dim receiveBytes As [Byte]() = MyUdpClient.Receive(RemoteIpEndPoint)      DstPort = RemoteIpEndPoint.Port     IpRemota(RemoteIpEndPoint.Address.ToString)     Dim BitDet As BitArray     BitDet = New BitArray(receiveBytes)     Dim strReturnData As String = System.Text.Encoding.ASCII.GetString(receiveBytes)     If UdpOpen Then         StartUdpReceiveThread(CInt(ListeningPortLbl.Text))     End If      PrintLog("From: " & RemoteIpLbl.Text & ":" & ListeningPortLbl.Text & " - " & strReturnData)     AnswersProcessor(strReturnData)  End Sub  Private Sub UdpSend(ByVal txtMessage As String)     Dim pRet As Integer     GLOIP = IPAddress.Parse(RemoteIpLbl.Text)     'From UDP_Server3_StackOv      Using UdpSender As New System.Net.Sockets.UdpClient()         Dim RemoteEndPoint = New System.Net.IPEndPoint(0, My.Settings.UDP_Port)         UdpSender.ExclusiveAddressUse = False         UdpSender.Client.SetSocketOption(Net.Sockets.SocketOptionLevel.Socket, Net.Sockets.SocketOptionName.ReuseAddress, True)         UdpSender.Client.Bind(RemoteEndPoint)         UdpSender.Connect(GLOIP, DstPort)         bytCommand = Encoding.ASCII.GetBytes(txtMessage)         pRet = UdpSender.Send(bytCommand, bytCommand.Length)     End Using      PrintLog("No of bytes send " & pRet) End Sub 

2 Answers

Answers 1

10013 is WSAEACCES, which is documented as follows:

Permission denied.

An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto without broadcast permission being set using setsockopt(SO_BROADCAST).

Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4.0 with SP4 and later), another application, service, or kernel mode driver is bound to the same address with exclusive access. Such exclusive access is a new feature of Windows NT 4.0 with SP4 and later, and is implemented by using the SO_EXCLUSIVEADDRUSE option.

Answers 2

In the comments you mentioned:

I tried the program on a XP x32 and works ok but on Windows 7 x32/x64 don't, even if I disable the firewall and Microsoft Security Essentials Live Protection.

Maybe it sounds almost obvious but you could try to start your program in all of the available Windows XP compatibility modes. You didn't say that you already tried this but maybe you're lucky and the problem will be "solved" by this workaround.

If the problem still exists afterwards and considering the error code of 10013, I would try or check the following things:

  • I know you disabled "Microsoft Security Essentials" and the Windows Firewall, but double check whether there are other security related programs/services like anti virus protection, anti malware tools etc. running. It really sounds like something is blocking your socket creation/bind.
  • In case your program created log output/data which allows you to see exactly when it started to fail:
    • Any new software installed at that time?
    • Were Windows Updates (maybe automatically) installed at that time? Especially security updates regarding network security?
    • Any other noticeable changes in your environment? What about log entries in your Windows system log?

  • Just as a little test to verify if the error occurs only with your UDP socket: Try to use a TCP socket instead of UDP.

  • Start the machine in Windows Safe Mode with network support and execute your program from there.

  • Run your program on another Windows 7 machine and see if the same problem occurs there. It could be a valuable starting point (in terms of localization) to know if the problem occurs only on specific versions of Windows.

  • Single step through your code with a debugger and carefully watch what happens. Perhaps this can reveal some additional info on what's going wrong.

Maybe some of the ideas above can help you to track down the problem a little bit more. Good luck!

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment