Using
UDP and Winsock in Visual Basic 6 (VB6) to send a Broadcast Message
I had an application where I wanted an administrator to be able to send a broadcast message to everyone running a particular VB program on the local area network (LAN).
I’ve done a lot of VB programming in the past and a fair amount of TCP Winsock programming, but this was new to me and quite frankly took me way too long to figure out.
Here’s how I did it:
Create one VB Project, on the form place two items: a textbox and a winsock control
This is the “receiving” application.
On the form place this code:
Option Explicit
Private Sub Form_Load()
Winsock1.Close
Winsock1.Protocol = sckUDPProtocol
Winsock1.Bind 420
End Sub
Private Sub Winsock1_DataArrival(ByVal
bytesTotal As Long)
Dim str As String
Winsock1.GetData str, vbString
Me.Text1.Text = str
End Sub
Private Sub Form_Unload(Cancel As
Integer)
Winsock1.Close
End Sub
Create one VB Project, on the form place two items: a textbox, a command button, and a winsock control
This is the “sending” application.
On the form place this code:
Option Explicit
Private Sub Command1_Click()
Winsock1.Close
Winsock1.Protocol = sckUDPProtocol
Winsock1.RemoteHost = "255.255.255.255"
Winsock1.RemotePort = 420
Winsock1.SendData "lvmsg" & Me.Text1.Text
End Sub
Private Sub Form_Load()
Winsock1.Bind 420
End Sub
Private Sub Form_Unload(Cancel As
Integer)
Winsock1.Close
End Sub
That’s about it. Just put whatever text you want into the text box and click on the button and it will be sent to the receiving application. Some of the details were missing on other web pages so I thought I would make it more clear here.