I have some windows software that manages multiple Linux boxes.
I wanted to add the ability to power up any of those boxes using WOL (Wake On Lan) so I wrote this UdpClient extension that adds an overloaded WakeFunction (see intellisense).
This code was translated (and simplified) from some existing C# examples found in various places. -
Imports System.Net.Sockets
-
-
''' <summary>
-
''' An extended UdpClient Class with WOL WakeFunction
-
''' </summary>
-
Public Class WOLClass
-
Inherits UdpClient
-
-
Public Sub New() : End Sub
-
-
Public Sub New(IPAddress As String, NetMask As String)
-
Me.IPAddress = IPAddress
-
Me.NetMask = NetMask
-
End Sub
-
-
Private Sub SetClientToBroadcastMode()
-
If Me.Active Then
-
Me.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, False)
-
End If
-
End Sub
-
-
Public Property IPAddress As New String(String.Empty)
-
-
Public Property NetMask As New String(String.Empty)
-
-
''' <summary>
-
''' Send a magic packet to the entire subnet based on IP and NetMask properties to the MAC Address and Port
-
''' </summary>
-
''' <param name="MACAddress">The MAC Address of the machine to waken</param>
-
''' <param name="Port">Remove port on destination system (default = 9)</param>
-
Public Function WakeFunction(MACAddress As String, Optional Port As Long = 9L) As Boolean
-
If Me.IPAddress.Length <> 0 AndAlso Me.NetMask.Length <> 0 Then
-
Return WakeFunction(Me.IPAddress, Me.NetMask, MACAddress, Port)
-
Else
-
Throw New Exception("IPAddress and NetMASK properties must be set before calling this overload")
-
End If
-
End Function
-
-
''' <summary>
-
''' Sends a magic packet to the entire subnet based on IP and NetMask with the target set to the MAC Address
-
''' </summary>
-
''' <param name="IPAddress">The IP Address to which the Magic Packet is directed (can be a broadcast address)</param>
-
''' <param name="NetMask">The Network Mask of the subnet</param>
-
''' <param name="MacAddress">The targets MAC address as xx:xx:xx:xx:xx</param>
-
''' <param name="Port">Remote port on destination system (default = 9)</param>
-
'''
-
Public Function WakeFunction(IPAddress As String, NetMask As String, MacAddress As String, Optional Port As Long = 9L) As Boolean
-
-
Dim BrdAddress As Net.IPAddress = GetBroadcastAddress(Net.IPAddress.Parse(IPAddress).ToString, NetMask)
-
Dim Counter As Integer = 0
-
Dim bytes(1023) As Byte
-
-
Using client As New WOLClass
-
-
client.Connect(BrdAddress, Port)
-
-
client.SetClientToBroadcastMode()
-
-
For I As Integer = 0 To 5
-
Counter = I
-
bytes(Counter) = &HFF
-
Next
-
-
Dim MacPieces As String() = MacAddress.Split({":"}, StringSplitOptions.RemoveEmptyEntries)
-
-
For I As Integer = 0 To 15
-
For J As Integer = 0 To 5
-
Counter += 1
-
bytes(Counter) = Byte.Parse(MacPieces(J), Globalization.NumberStyles.HexNumber)
-
Next
-
Next
-
-
Try
-
client.Send(bytes, 1024I)
-
Return True
-
Catch ex As Exception
-
Throw New Exception(ex.Message)
-
End Try
-
End Using
-
-
End Function
-
-
''' <summary>
-
''' Returns the subnets broadcast address based on the provided IP Address and Subnet Mask
-
''' </summary>
-
''' <param name="IPAddress">Any IP in the subnet (can even be the current broadcast address)</param>
-
''' <param name="NetMask">The NetMask of the the subnet</param>
-
''' <returns></returns>
-
Private Function GetBroadcastAddress(IPAddress As String, NetMask As String) As Net.IPAddress
-
-
Dim ThisAddress As Net.IPAddress = Net.IPAddress.Parse(IPAddress)
-
Dim ThisMask As Net.IPAddress = Net.IPAddress.Parse(NetMask)
-
-
Dim IPPieces As String() = IPAddress.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
-
Dim MaskPieces As String() = NetMask.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
-
-
Dim IPBytes As Byte() = ThisAddress.GetAddressBytes
-
Dim MaskBytes As Byte() = ThisMask.GetAddressBytes
-
Dim BroadcastAddress(3) As Byte
-
-
For I As Integer = 0 To (IPBytes.Length - 1)
-
BroadcastAddress(I) = CByte((IPBytes(I) Or (MaskBytes(I) Xor 255)))
-
Next
-
-
Return New Net.IPAddress(BroadcastAddress)
-
-
End Function
-
End Class
-
Example Usage -
Using wc As New WOLClass("192.168.1.178", "255.255.255.0")
-
If wc.WakeFunction("ac:1f:6b:20:11:30") Then
-
MsgBox("Magic Packet Sent", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
-
End If
-
End Using
-
0 4690 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fernando Rodriguez |
last post by:
Hi,
I need to traverse the methods defined in a class and its superclasses. This
is the code I'm using:
# An instance of class B should be able to check all the methods defined in B
#and A,...
|
by: QH Hong |
last post by:
Hi, I have several classes and several members.
class c1, c2, c3,...
member of class c1 is m11, m12,...
member of class c2 is m21, m22,...
Each member takes one argument (double type) and...
|
by: Bj?rn Toft Madsen |
last post by:
Hi all,
The network library I use communicates with my app using a callback
function. This callback function is called with the type of message, a
void pointer to he actual message and a user...
|
by: Firstname Lastname |
last post by:
In a function call, for example function(Class(), &Class::memfunc), my
compiler cannot match the 2nd argument when: (1) Class is a template
parameter, and (2) the base class of Class defines...
|
by: hazz |
last post by:
I have a class/constructor hiearchy that functions correctly as illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new...
|
by: Steve |
last post by:
Hello --
I've been struggling with this problem for over a day now. I'd like to
know (without passing parameters) which class, and preferably, which
method of that class has called my function....
|
by: Jef Driesen |
last post by:
Suppose I have a datastructure (actually it's a graph) with one template
parameter (the property P for each edge and vertex):
struct graph<P>;
struct vertex<P>;
struct edge<P>;
I also have...
|
by: mongolian |
last post by:
Hello,
I am trying to execute an Extended Stored Procedure in a User Defined Function I wrote. It really is quite simple but I get an error saying
"Only functions and extended stored procedures...
|
by: divya_rathore_ |
last post by:
Why doesn't the following code work for a project? How
else can I assign the 'class' keyword a value from outside (not
embedded within the asp.net code, say as in: class=class_red)?
//some C#...
|
by: PengYu.UT |
last post by:
Hi,
Are there any walkaround to enable functions in the derived class with
the same function name but different return type?
In the following example, D1 and D2 are B's derived class. I want...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |