vb.net UDP广播显示来自发件人的IP

时间:2013-10-17 17:40:51

标签: c# wpf vb.net sockets ip

我发送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地址并在Messagebox,textbox等中显示

我看到了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并使用上面的代码在文本框,消息等中显示它...... ????

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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