C# vb.net UDP广播显示来自发送方的IP

C# vb.net UDP广播显示来自发送方的IP,c#,wpf,vb.net,sockets,ip,C#,Wpf,Vb.net,Sockets,Ip,我正在使用以下代码发送UDP广播,其中包含消息“Hello?”: 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.Lengt

我正在使用以下代码发送UDP广播,其中包含消息“Hello?”:

    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
当然,我的用户可以很好地接收信息

  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等中显示它呢

我看到了
套接字。receivefrom
方法,但是!我收到此错误
“系统在尝试在调用中使用指针参数时检测到无效的指针地址”
在下面代码的这一点,“
s.ReceiveFrom(msg,SocketFlags.None,senderRemote)


因此,我如何获取发送方IP并使用上面的代码在文本框、消息等中显示它…?

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

我只需要使用IPEndPoint.ToString方法:

那么如何将IP转换为字符串?我对如何获取UDP广播的发送者的IP感到困惑。我尝试了一个ToString,但不起作用。Dim seeIP作为String ip.ToString(seeIP)对不起,我误读了很多关于你的原始代码。您需要在
BeginReceive
调用中发送udp状态。我已经更新了我的原始答案代码。又一次编辑。我没有意识到UdpState不是一个.NET类。在我使用C#时,您必须原谅我的vb.net技能,但您需要一个简单的类(
UdpState
)来保存您的udp信息。我不知道我写的那封信是否正确,但它会让你走上正确的方向。你很好!谢谢你可以用C#I use converter.telerik.com来写/‎ 这在转化方面做得很好。
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