Benjamin Jones
Benjamin Jones

Reputation: 997

vb.net UDP broadcast show IP from sender

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....????

Upvotes: 0

Views: 4328

Answers (2)

Michael Mims
Michael Mims

Reputation: 250

BeginReceive is an asynchronous call. You need to set up your udp connection before you call BeginReceive. I don't know vb.net very well (I use C#), but you need to use something roughly like this:

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

Upvotes: 1

Benjamin Jones
Benjamin Jones

Reputation: 997

All I needed to do was use IPEndPoint.ToString Method: http://msdn.microsoft.com/en-us/library/system.net.ipendpoint.tostring.aspx

Upvotes: 0

Related Questions