0

私は私に2つの問題を与え続けるこのコードを持っています。

最初

要求されたアドレスはそのコンテキストでは無効です

第二に、それはそれが送る放送を受信します、私はこれを望んでいません。リスニングサーバーアプリのみにブロードキャストを受信させたい

送信コード

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

この問題を解決するにはどうすればよいですか?

4

1 に答える 1

1

まず、IPAddress.Anyではなく、実際のサブネットIPアドレスにブロードキャストする必要があります。

第二に、重複したパケットを回避することはできません。ブロードキャストソケットは、ブロードキャストするのと同じパケットを受信することになっています。それは放送の仕組みの一部です。送信者のIPアドレスをブロードキャストIPアドレスと比較して不要なパケットを除外し、それらが一致するかどうかを確認する必要があります。

于 2011-08-24T01:57:46.387 に答える