Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim WaitThread As Threading.Thread
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Show()
Application.DoEvents()
WaitThread = New Threading.Thread(AddressOf Wait4Command)
WaitThread.IsBackground = True
WaitThread.Start()
End Sub
Private Sub ReceiveCommandX(ByVal params As String)
'// this Sub receives the commands (from Wait4Command)
TextBox1.Text = params
ListBox1.Items.Add(params)
End Sub
Delegate Sub ReceiveCommand(ByVal params As String)
Sub Wait4Command()
Dim udpClient As New UdpClient(5000)
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes As [Byte]()
Dim returnData As String
Try
While True
receiveBytes = UdpClient.Receive(RemoteIpEndPoint)
returnData = RemoteIpEndPoint.Address.ToString() & ": " & Encoding.ASCII.GetString(receiveBytes)
Me.Invoke(New ReceiveCommand(AddressOf ReceiveCommandX), returnData)
End While
Catch e As Exception
MsgBox(e.ToString)
End Try
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim o As New Object
o = TextBox2.Text
Dim s As String = DirectCast(o, String)
Dim p As New Net.IPEndPoint(Net.IPAddress.Broadcast, 6000)
Dim u As New UdpClient()
u.ExclusiveAddressUse = False
u.EnableBroadcast = True
u.DontFragment = True
u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
u.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, True)
u.Client.Bind(New Net.IPEndPoint(Net.IPAddress.Any, 6000))
Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(s)
u.Send(b, b.Length, p)
u.Close()
End Sub
End Class