Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Already running application now gets socket error 10013

Tags:

vb.net

sockets

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
like image 828
E_Blue Avatar asked Sep 14 '16 13:09

E_Blue


People also ask

How do I fix Socket Error 10013?

Socket error 10013 message implies that a port is blocked and/or unreachable. To fix it, you can try turning off any security software (such as antivirus and firewall product) installed on your server.

What WinError 10013?

OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions.

How do I fix Wsaeacces?

To work around this issue, use one of the following methods: Use a port that is not included in the default dynamic port range (from 49,152 to 65,535), and do not specify the port as an excluded port by running the netsh command.


1 Answers

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.

like image 107
Remy Lebeau Avatar answered Nov 15 '22 07:11

Remy Lebeau