Broadcasting is frequently used in
local area networks.
If you want to send a message to all local computers you
can use broadcasts. Here we will discuss about broadcasting
on TCP/IP network protocol.
For example if you want to find a resource on network you can
send a broadcast to all computers. Then computer(s) that
contain that resource can answer to you.
You can find several applications for broadcasting. Just be
sure to use broadcasting whenever it is really needed.
To send broadcasts you use UDP protocol to send packets to IP
address 255.255.255.255 and you can choose any port that is
free on your computers.
Sending and receiving UDP broadcasts is very easy. You will
not need a connect session for UDP. You just send and receive
packets. Also packet size limitation is related to your
network devices but assume 256 bytes for maximum size of data
you send each time.
Below you see a sample program written in Visual Basic using
MSWinsock.ocx component. This component is the most stable socket
object I have seen until now. It is very easy to program sockets
with this object. I even prefer this object to native Delphi
and C++Builder socket objects (Borland, Net Master and Indy)
in that environments.
Private Sub Command1_Click()
On Local Error Resume Next
Dim str As Variant
str = CVar(Text1.Text)
Winsock1.RemoteHost = "255.255.255.255"
Winsock1.RemotePort = 20480
Winsock1.SendData str
End Sub
Private Sub Form_Load()
Winsock1.Bind 20480
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData str, vbString
Text2.Text = str
End Sub
Form contains a command button and two text boxes.
If you want to write in other environment there is a Broadcast
Page at this address:
http://www.angelfire.com/nt/sarmadys/page3.html
|