Udpclient有时会抛出异常

时间:2014-11-05 08:53:42

标签: vb.net exception udpclient

我目前正在开发UDP通信PC< - > ARM LM3S6965(Luminary)通过以太网。在PC上有一个模拟UDP服务器/客户端的VB.net应用程序。

消息从Vb.net应用程序广播到基于ARM LM3S6965的设备,并且返回响应从ARM设备发送到vb.net应用程序。

但有时会收到UdpClient抛出套接字异常,即通常只允许使用每个套接字地址(协议/网络地址/端口)。

抛出异常的行是

udpReceivingClient = New UdpClient(mnPort)

完整的VB.net代码如下

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 > 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

代码中有什么错误?我如何使用相同的端口进行接收和发送?

0 个答案:

没有答案