using sockets

  • Thread starter Thread starter Starbuck
  • Start date Start date
S

Starbuck

Hi
I want to broadcast a message to all pc's on a lan via port 1002.
Can anyone tell me the best way of doing a general broadcast.

Thanks in advance
 
you can use the below class like this , if you set strIpadress to
"255.255.255.255" the message will be sent to all computers on the network


clsBc = New Broadcaster(strIpAdress,
CType(Integer.Parse(ConfigurationSettings.AppSettings("BCServerPort")),
Short), strmessage)

clsBc.SendMessage()

clsBc = Nothing



### start class code ###
Imports System.Net

Imports System.Net.Sockets

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()


_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)

RaiseEvent MessageSent()

Catch ex As System.Net.Sockets.SocketException


RaiseEvent MessageFailed()

End Try

End Sub

#End Region

End Class



### end class code ###


regrds

Michel Posseth
 
Thanks


m.posseth said:
you can use the below class like this , if you set strIpadress to
"255.255.255.255" the message will be sent to all computers on the network


clsBc = New Broadcaster(strIpAdress,
CType(Integer.Parse(ConfigurationSettings.AppSettings("BCServerPort")),
Short), strmessage)

clsBc.SendMessage()

clsBc = Nothing



### start class code ###
Imports System.Net

Imports System.Net.Sockets

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()


_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)

RaiseEvent MessageSent()

Catch ex As System.Net.Sockets.SocketException


RaiseEvent MessageFailed()

End Try

End Sub

#End Region

End Class



### end class code ###


regrds

Michel Posseth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top