title: Udpclient sometimes throws exception
body: <p>I'm currently working on a UDP communication PC &lt;-> ARM LM3S6965 (Luminary) through the Ethernet. On the PC there is a VB.net application that simulates a UDP server/client.</p>
  
  <p>the message is broadcasted from Vb.net application to ARM LM3S6965 based device, and in return response is sent from ARM Device to vb.net application.</p>
  
  <p>but sometimes receive UdpClient throws socket exception i.e. Only one usage of each socket address (protocol/network address/port) is normally permitted.</p>
  
  <p>the line which throws exception is</p>
  
  <pre><code>udpReceivingClient = New UdpClient(mnPort)
  </code></pre>
  
  <p>Complete VB.net Code is as follows</p>
  
  <pre><code>Module mod_Search_UDP
  Public mnPort As Int32 = 3040                                   'Port number to send/recieve data on
  'Public Const msBroadcastAddress As String = "255.255.255.255"   'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
  'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
  Public udpReceivingClient As UdpClient                          'Client for handling incoming data
  Public udpSendingClient As UdpClient                            'Client for sending data
  Public receivingThread As Thread                                'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
  Public mbiClosing As Boolean = False                            'Used to close clients if form is closing
  Public mbiCloseRxClient As Boolean = False
  
  Public Sub InitializeSender()
      Dim soc As Socket
      Dim lsPort As String
      Dim lnPort As Int32 = 3040
      Const lsBroadcastAdd As String = "255.255.255.255"
      'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
      'udpSendingClient = New UdpClient(endPoint)
      udpSendingClient = New UdpClient(lsBroadcastAdd, lnPort)
  
      udpSendingClient.EnableBroadcast = True
  
      soc = udpSendingClient.Client
      lsPort = (CType(soc.LocalEndPoint, IPEndPoint).Port.ToString())
      mnPort = Convert.ToInt32(lsPort)
  End Sub
  
  Public Sub InitializeReceiver()
      'Create UdpClient class and bind it to the local port number provided
      'Try
      udpReceivingClient = New UdpClient(mnPort)
      'udpReceivingClient.EnableBroadcast = True
      mbiCloseRxClient = True
      'Catch ex As Exception
      '    MsgBox(ex.ToString)
      'End Try
  
  
      Dim start As ThreadStart = New ThreadStart(AddressOf MT_Receiver)
      receivingThread = New Thread(start)
      receivingThread.IsBackground = True
      receivingThread.Start()
  End Sub
  
  Public Sub MT_Receiver()
  
      Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, mnPort)    'Listen for incoming data from any IP address on the specified port 
      Dim lbData() As Byte                                                'Buffer for storing incoming bytes
      Dim llRet As UInt16
      'udpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
      While (True)                                                        'Setup an infinite loop
          If mbiClosing = True Then                                       'Exit sub if form is closing
              Exit Sub
          End If
          llRet = udpReceivingClient.Available
          If llRet &gt; 0 Then
              lbData = udpReceivingClient.Receive(endPoint)                   'Receive incoming bytes
              'If udpListen.Available Then
              '    udpListen.Receive(lbData, 256, Net.Sockets.SocketFlags.None)
              'End If
              'If lbData Is Nothing Then
              'Else
              frmSearchUDP.MT_Validate_Msg(lbData)
          End If
      End While
  End Sub
  Public Sub MT_Send_UDP(ByVal lbTxBuffer() As Byte)
      InitializeSender()
      If mbiCloseRxClient = True Then
          receivingThread.Abort()
          udpReceivingClient.Client.Dispose()
          udpReceivingClient.Close()
  
      End If
      Try
          udpSendingClient.Send(lbTxBuffer, lbTxBuffer.Length)
      Catch ex As Exception
          MsgBox(ex.ToString)
      End Try
      udpSendingClient.Client.Dispose()
      udpSendingClient.Close()
      InitializeReceiver()
      'Try
      '    udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing)
      'Catch ex As Exception
      '    MsgBox(ex.ToString)
      'End Try
  End Sub
  End Module
  </code></pre>
  
  <p>Whats mistake is there in code ? how can i use same port for receive and transmit ?</p>