I have an application running in VB.NET that listens on a specific UDP port and responds through the same port to the IP address that sends the packet. It worked fine from two years until the last month; now that you are trying to respond to a failure due to socket error 10013.
I even try to use an older version, and I know that it works too and gets the same crash.
I am trying to disable real-time protection of Microsoft Security Essentials and the Windows Firewall and it is not working.
In the code, I have a line
MyUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
I have no idea what to do, I'm lost.
Any idea how to solve this?
Edit: Here is 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
source share