udpclient接收广播问题

发布于 2024-12-01 05:11:49 字数 1978 浏览 1 评论 0原文

我的这段代码一直给我带来两个问题。

第一的

请求的地址在其上下文中无效

第二,它接收它发送的广播,我不想要这个。我只想让监听服务器应用程序接收广播

发送代码

Dim sendMessage As New structMessage
        sendMessage.Command = Command.IP
        Dim byteData As Byte() = sendMessage.ToByte()
        'Using UDP sockets

        epServer = New IPEndPoint(IPAddress.Any, iCurrUDPPort)

        'sckClientUDP.EnableBroadcast = True
        sckClientUDP.EnableBroadcast = True
        sckClientUDP.BeginSend(byteData, byteData.Length, _
                               CType(epServer, Net.IPEndPoint), _
                                New AsyncCallback(AddressOf sckClientUDP_DataArrival), _
                                Nothing)


        '## if server not found , increment port
        If iCurrUDPPort = iToPort Then
            iCurrUDPPort = iFromPort
        Else
            iCurrUDPPort = iCurrUDPPort + 1
        End If

接收代码

    Private Sub sckClientUDP_DataArrival(ByVal ar As IAsyncResult)
        Try
            Dim remoteEP As EndPoint = Nothing
            sckClientUDP.EndReceive(ar, CType(remoteEP, IPEndPoint))
            'Convert the bytes received into an object of type Data
            Dim recvMessage As New structMessage(byteData)
            'Accordingly process the message received
            Select Case recvMessage.Command
                Case Command.IP
                    ServerIP = recvMessage.IP
                    ServerPort = recvMessage.Port
                    ' try connect here (TCP)
            End Select

            byteData = New Byte(1023) {}

            'Start listening to receive more data from the user
            sckClientUDP.BeginReceive(New AsyncCallback(AddressOf sckClientUDP_DataArrival), Nothing)
        Catch generatedExceptionName As ObjectDisposedException
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
end sub

我该如何解决这个问题?

I have this code that keeps giving me two problems.

first

The requested address is not valid in its context

second , it receives broadcast it sends, i dont want this. I want only the listeneing server app to receive the broadcast

the sending code

Dim sendMessage As New structMessage
        sendMessage.Command = Command.IP
        Dim byteData As Byte() = sendMessage.ToByte()
        'Using UDP sockets

        epServer = New IPEndPoint(IPAddress.Any, iCurrUDPPort)

        'sckClientUDP.EnableBroadcast = True
        sckClientUDP.EnableBroadcast = True
        sckClientUDP.BeginSend(byteData, byteData.Length, _
                               CType(epServer, Net.IPEndPoint), _
                                New AsyncCallback(AddressOf sckClientUDP_DataArrival), _
                                Nothing)


        '## if server not found , increment port
        If iCurrUDPPort = iToPort Then
            iCurrUDPPort = iFromPort
        Else
            iCurrUDPPort = iCurrUDPPort + 1
        End If

The receiving code

    Private Sub sckClientUDP_DataArrival(ByVal ar As IAsyncResult)
        Try
            Dim remoteEP As EndPoint = Nothing
            sckClientUDP.EndReceive(ar, CType(remoteEP, IPEndPoint))
            'Convert the bytes received into an object of type Data
            Dim recvMessage As New structMessage(byteData)
            'Accordingly process the message received
            Select Case recvMessage.Command
                Case Command.IP
                    ServerIP = recvMessage.IP
                    ServerPort = recvMessage.Port
                    ' try connect here (TCP)
            End Select

            byteData = New Byte(1023) {}

            'Start listening to receive more data from the user
            sckClientUDP.BeginReceive(New AsyncCallback(AddressOf sckClientUDP_DataArrival), Nothing)
        Catch generatedExceptionName As ObjectDisposedException
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
end sub

How do i fix this problems?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-12-08 05:11:49

首先,您应该广播到实际的子网 IP 地址,而不是 IPAddress.Any。

其次,你无法避免重复的数据包。广播套接字应该接收它广播的相同数据包。这是广播工作方式的一部分。您必须通过将发件人的 IP 地址与您的广播 IP 地址进行比较以查看它们是否匹配来过滤掉任何不需要的数据包。

First, you should be broadcasting to an actual subnet IP address, not to IPAddress.Any.

Second, you cannot avoid the duplicated packet. The broadcasting socket is supposed to receive the same packet it broadcasts. That is part of how broadcasting works. You will have to filter out any unwanted packets by comparing their sender's IP address to your broadcasting IP address to see if they match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文