【问题标题】:vb.net UDP broadcast show IP from sendervb.net UDP广播显示来自发件人的IP
【发布时间】:2013-10-26 08:19:44
【问题描述】:

我正在发送带有消息“你好?”的 UDP 广播。使用此代码:

    Public Sub UDPSendHello()

    Dim client As New UDPClient()
    Dim ip As New IPEndPoint(IPAddress.Broadcast, 15000)

    Dim bytes As Byte() = Encoding.ASCII.GetBytes("Hello?")
    client.Send(bytes, bytes.Length, ip)
    client.Close()
End Sub

当然,UDPListner 可以正常接收消息!

  Private ReadOnly udp As New UdpClient(15000)
    Public Sub UDPHelloListner()
        udp.BeginReceive(AddressOf Receive, New Object())
    End Sub
    Private Sub Receive(ByVal ar As IAsyncResult)
        Dim ip As New IPEndPoint(IPAddress.Any, 15000)
        Dim bytes As Byte() = udp.EndReceive(ar, ip)
        Dim message As String = Encoding.ASCII.GetString(bytes)
        If message = "Hello?" Then
            Dim sender As New IPEndPoint(IPAddress.Any, 15000)
            Dim senderRemote As EndPoint = CType(sender, EndPoint)

            MessageBox.Show("I see message, Hello")
        End If

        UDPHelloListner()
    End Sub

那么我怎样才能获得发件人的 IP 地址并将其显示在消息框、文本框等中。

我看到了Socket.RecieveFrom 方法,但是!在以下代码的这一点上,我收到此错误"The system detected an invalid pointer address in attempting to use a pointer argument in a call",“s.ReceiveFrom(msg, SocketFlags.None, senderRemote)

   Public Sub ReceiveFrom2()
        Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
        Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)

        Dim s As New Socket(EndPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)

        ' Creates an IpEndPoint to capture the identity of the sending host. 
        Dim sender As New IPEndPoint(IPAddress.Any, 0)
        Dim senderRemote As EndPoint = CType(sender, EndPoint)

        ' Binding is required with ReceiveFrom calls.
        s.Bind(endPoint)

        Dim msg() As Byte = New [Byte](255) {}
        Console.WriteLine("Waiting to receive datagrams from client...")
        ' This call blocks. 
        s.ReceiveFrom(msg, SocketFlags.None, senderRemote)
        s.Close()

    End Sub 'ReceiveFrom2

那么我将如何获取发件人 IP 并使用上面的代码在文本框、消息等中显示它......????

【问题讨论】:

    标签: c# wpf vb.net sockets ip


    【解决方案1】:

    BeginReceive 是一个异步调用。在致电BeginReceive 之前,您需要设置您的 udp 连接。我不是很了解vb.net(我用的是C#),但你需要使用大致像这样的东西:

    Public Class UdpState
       Public e As IPEndPoint
       Public u As UdpClient
    End Class
    
    Public Shared messageReceived As Boolean = False
    
    Public Sub UDPHelloListner()
        Dim ip As New IPEndPoint(IPAddress.Any, 15000)
        Dim udp As New UdpClient(ip)
    
        Dim state As New UdpState()
        state.e = ip
        state.u = udp
    
        udp.BeginReceive(new AsyncCallback(AddressOf Receive), state)
    
        Do While Not messageReceived
            Thread.Sleep(100)
        Loop
    End Sub
    
    Private Sub Receive(ByVal ar As IAsyncResult)
        Dim udp As UdpClient = CType((CType(ar.AsyncState, UdpState)).u, UdpClient)
        Dim ip As IPEndPoint = CType((CType(ar.AsyncState, UdpState)).e, IPEndPoint)
    
        Dim bytes As Byte() = udp.EndReceive(ar, ip)
        Dim message As String = Encoding.ASCII.GetString(bytes)
        If message = "Hello?" Then
            MessageBox.Show("I see message, Hello from {0}", ip.Address.ToString())
            messageReceived = True
        End If
    End Sub
    

    【讨论】:

    • 那么如何将IP转换为字符串呢?我很困惑如何获取 UDP 广播的发送者的 IP
    • 尝试了 ToString,但不起作用。将 seeIP 调暗为字符串 ip.ToString(seeIP)
    • 对不起,我误读了很多关于您的原始代码的内容。您需要在 BeginReceive 调用中发送 udp 状态。我已经更新了我原来的答案代码。
    • 又一次编辑。我没有意识到 UdpState 不是 .NET 类。当我使用 C# 时,您必须原谅我的 vb.net 技能,但您需要一个简单的类 (UdpState) 来保存您的 udp 信息。我不知道我写的是否正确,但它应该能让你朝着正确的方向前进。
    • 你很好!谢谢!你总是可以用 C# 编写它,我使用 converter.telerik.com/‎,它在转换方面做得很好。
    【解决方案2】:

    我需要做的就是使用 IPEndPoint.ToString 方法: http://msdn.microsoft.com/en-us/library/system.net.ipendpoint.tostring.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-28
      • 2010-09-28
      • 2016-07-26
      • 2019-06-17
      • 1970-01-01
      • 2012-02-03
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多