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