0

I am sending a UDP broadcast out with the message "Hello?" using this code:

    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

Of couse I the UDPListner can recieve the message fine!

  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

Well how can I get the senders IP address and show it in Messagebox,textbox,etc.

I saw the Socket.RecieveFrom Method, but! I am getting this error "The system detected an invalid pointer address in attempting to use a pointer argument in a call" at this point of the below code, " 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

So how would I get the sender IP and show it using the code above in a textbox,message,etc....????

4

2 回答 2

1

BeginReceive是一个异步调用。您需要在调用之前设置您的 udp 连接BeginReceive。我不太了解 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
于 2013-10-17T17:52:58.807 回答
0

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

于 2013-10-17T18:04:43.590 回答