Thursday, February 8, 2007

Broadcast UDP Message Vb.Net

If you want to send something to every machine on a Subnet, there's a handy mechanism called a Broadcast. Nothing that your broadcast can get outside of the network, so it's an internal mechanism. It's important to point out that this isn't a 'popup' message or anything visual. Rather, if you have a listener on that port, it will be received by it. What it does with that message is up to the client app.

This code is pretty self explanatory, but let me run through a few things. 1) I arbitratiry chose a port, in real life it may be being used or blocked so make sure you run it by your Network Admin before playing with his ports. 2) Use 255.255.255.255 to send it out to everyone, or use your network IP and append 255. Hence if your IP is 205.183.31 then add .255 to it 205.183.31.255 3) Somehow the consumer will want to know if things worked or not...Instead of using Return codes, I decided to raise some events. However, I could have used both, but since this is such a simple class, I got a little lazy.

First I pull in the Imports Statements:

Imports System.Net
Imports System.Net.Sockets


Next, here's the class:

Public Class Broadcaster
#Region "Delegates"
Delegate Sub MessageSuccess()
Delegate Sub MessageFailure()
#End Region

#Region "Private Fields"
Private _NetIPAddress As String
Private _Port As Int16
Private _BroadcastMessage As String
Private myClient As New System.Net.Sockets.UdpClient
Private _Info As Byte()
'Points to MessageSuccess()
Public Event MessageSent As MessageSuccess
'Points to MessageFailure
Public Event MessageFailed As MessageFailure
#End Region

#Region "Properties"
Public Property NetIPAddress() As String
Get
Return _NetIPAddress
End Get
Set(ByVal Value As String)
_NetIPAddress = Value
End Set
End Property

Public Property Port() As Int16
Get
Return _Port
End Get
Set(ByVal Value As Int16)
_Port = Value
End Set
End Property
Public Property BroadcastMessage() As String
Get
Return _BroadcastMessage
End Get
Set(ByVal Value As String)
_BroadcastMessage = Value
End Set
End Property
#End Region

#Region "Methods"
'If this constructor is used, all you need to do is call SendMessage
Public Sub New(ByVal IP_Address As String, ByVal PortNumber As Int16, ByVal Msg As String)
Me.NetIPAddress = IP_Address
Me.Port = PortNumber
Me.BroadcastMessage = Msg
End Sub
'If this constructor is used, make sure you set the BroadcastMessage
Public Sub New(ByVal IP_Address As String, ByVal PortNumber As Int16)
Me.NetIPAddress = IP_Address
Me.Port = PortNumber
End Sub

Public Sub SendMessage()
'To make this more robust, I should probably check
'that there is in fact a message and respond accordingly..
'but it's Sunday night so forgive me.
_Info = System.Text.Encoding.UTF8.GetBytes(Me.BroadcastMessage)
Dim EndPoint As New IPEndPoint(IPAddress.Parse(Me.NetIPAddress), Me.Port)
Try
myClient.Send(Me._Info, Me._Info.Length, EndPoint)
'Use a Success Event and raise it if things worked
RaiseEvent MessageFailed()
Catch ex As System.Net.Sockets.SocketException
'Instead of using a return type, why not just create
'a Failed Event?
RaiseEvent MessageSent()
End Try
End Sub
#End Region
End Class


Finally, to use the class with the 3 Paramater constructor:

Dim SomeBroadCaster As New Broadcaster("255.255.255.255", 2000, "I Am Babatunde")
SomeBroadCaster.SendMessage()

No comments: