在发送后,无法在VB.Net中从UDPClient异步接收数据

时间:2019-02-04 19:05:57

标签: vb.net udpclient

我想编写一些VB.NET代码,该代码在广播中发送一些字节,并从发送时未知的各种设备接收应答:唯一已知的是广播发送的端口号,这些设备正在侦听的端口

我暂时可以使这项工作正常进行的唯一方法是同步(确切地说,是异步方法调用并同步等待答案,所以这是同步方式),这是一个问题,因为在此期间我的应用程序被阻止了

这是实际起作用的代码:

        broadcastIPEP = New IPEndPoint(IPBroadcast_, port_)
        'Create Broadcaster
        UDPClient = New UdpClient With {.EnableBroadcast = True}
        'Send array of bytes 
        nbBytesSent = UDPClient.Send(bytTab, bytTab.Length, broadcastIPEP.Address.ToString, broadcastIPEP.Port)
        'Get the number of the port to listen
        port = DirectCast(UDPClient.Client.LocalEndPoint, IPEndPoint).Port
        'Close broadcaster
        UDPClient.Close()
        'Create new listener on port
        UDPClient = New UdpClient(port)
        While True
            nbMsWait = 0I
            'Start receive
            ar = UDPClient.BeginReceive(Nothing, Nothing)
            'Wait answer until it comes
            Do
                nbMsWait += DELAY_WAIT_RESPONSE
                If nbMsWait > DELAY_MAX_WAIT_RESPONSE Then
                    Exit Try
                End If
                ar.AsyncWaitHandle.WaitOne(DELAY_WAIT_RESPONSE)
            Loop Until ar.IsCompleted
            'Read answer
            bytTab = UDPClient.EndReceive(ar, ipRemote)
            strMsg = Text.Encoding.UTF8.GetString(bytTab)
            'Process received message
            ProcessResponse(strMsg)
        End While

它工作正常,但是就像我说的那样,问题在于它实际上不是异步的。 我所有使它以异步方式运行的尝试都行不通,因为当我异步执行此操作时,数据永远不会进入UDPClient。我已经尝试了很多方法: 以纯异步方式使用异步方法,诸如此类:

broadcaster = New UdpClient With {.EnableBroadcast = True}
broadcaster.BeginSend(BYTES_TO_SEND, NB_BYTES_TO_SEND, broadcastIPEP, New AsyncCallback(AddressOf OnDataSent), broadcaster)

这是发送回调

Protected Sub OnDataSent(ar As IAsyncResult)

    'Local variables
    Dim broadcaster As UdpClient = Nothing, client As UdpClient = Nothing
    Dim port As Integer = 0I

    Try
        broadcaster = DirectCast(ar.AsyncState, UdpClient)
        port = DirectCast(broadcaster.Client.LocalEndPoint, IPEndPoint).Port
        broadcaster.Close()
        client = New UdpClient(port)
        'Schedule the next receive operation once reading is done:
        client.BeginReceive(New AsyncCallback(AddressOf OnDataReceived), client)
    Catch ex As Exception
        ProcessError(ex)
    End Try

End Sub

这是接收回调:

Protected Sub OnDataReceived(ar As IAsyncResult)

    'Local variables
    Dim client As UdpClient = Nothing
    Dim source As IPEndPoint = Nothing
    Dim bytTab() As Byte = Nothing
    Dim strMsg As String = String.Empty

    Try
        'This is what had been passed into BeginReceive as the second parameter:
        client = DirectCast(ar.AsyncState, UdpClient)
        'Points towards whoever had sent the message:
        source = New IPEndPoint(0, 0)
        'Get the actual message and fill out the source:
        bytTab = client.EndReceive(ar, source)
        'Process received message
        strMsg = Encoding.GetString(bytTab)
        ProcessResponse(strMsg)
        'Schedule the next receive operation once reading is done:
        client.BeginReceive(New AsyncCallback(AddressOf OnDataReceived), client)
    Catch ex As Exception
        ProcessError(ex)
    End Try

End Sub

执行进入发送回调,但从不进入接收回调。

我尝试在单独的线程中使用同步方法,但是存在相同的问题:无法接收任何东西,而且我确定会有东西要接收。这是线程例程的代码:

    'Local Variables
    Dim nbBytes As Integer = 0I
    Dim bytTab() As Byte = Nothing
    Dim strMsg As String = String.Empty

    Try
        'Get broadcast address
        broadcastIPEP = DirectCast(IP_, IPEndPoint)
        'Create broadcaster
        UDPBroadcaster = New UdpClient With {.EnableBroadcast = True}
        'Set broadcast address to broadcaster
        UDPBroadcaster.Connect(broadcastIPEP)
        'Send broadcast
        nbBytes = UDPBroadcaster.Send(BYTES_TO_SEND, NB_BYTES_TO_SEND)
        'Get the port to listen
        listenPort = DirectCast(UDPBroadcaster.Client.LocalEndPoint, IPEndPoint).Port
        'Close broadcaster
        UDPBroadcaster.Close()
        'Create listener
        UDPListener = New UdpClient(listenPort)
        'Create listening address
        listenIPEP = New IPEndPoint(IPAddress.Any, listenPort)
        'listening loop
        While booListen
            Thread.Sleep(10I)
            If booNotify Then
                Continue While
            End If
            bytTab = UDPListener.Receive(listenIPEP)
            strMsg = Text.Encoding.UTF8.GetString(bytTab)
            ProcessResponseAsync(strMsg)
        End While
    Catch ex As Exception
        ProcessError(ex)
    End Try

有人可以告诉我为什么无法使用UDPClient异步接收吗? 感谢所有将提供帮助的人

0 个答案:

没有答案