Maverick1182 |
12-05-2006 08:02 AM |
Incorporating .NET code into a webpage
I have finally got my wake on LAN function working by using the depicus website WOL ovre internet http://www.depicus.com/wake-on-lan/woli.aspx (for those who are interested). I'm being a little paranoid and I want to make a similar webpage which does the same thing just incase the Depicus website closes down etc. On the depicus page it gives a stripped down version of .NET code for creating the magic packet. I'm not a hardcore programmer and I tried to copy the code to my website (at uni). Unfortunately I dont know how to manipulate the .NET code to make it into the usable type of code as on the depicus page. I was wondering if anyone who has the experience had any suggestions (or could help) on how to create the webpage page. I may invest time in learning html / .net from scratch but I dont know if that's over kill. Hence i need to have someone look at it before I commence. Any advice would be much appreciated.
The code is as follows:
Code:
<%@ Import Namespace = "System.Net" %>
<%@ Import Namespace = "System.Net.Sockets" %>
<%@ Import Namespace = "System.Net.Dns"%>
<%@ Import Namespace = "System.Text"%>
<script language="vb" runat="server">
Private Function InvertBinary(ByVal x As String) As String
Dim ch As Char
Dim len As Integer = CStr(x).Length
For Each ch In CStr(x)
If ch = "1" Then
InvertBinary += "0"
Else
InvertBinary += "1"
End If
Next
End Function
Private Function OrIt(ByVal x As Long, ByVal y As Long) As String
'Pad out
Dim xx As String
xx = CStr(x)
While xx.Length < 8
xx = "0" + xx
End While
Dim yy As String
yy = CStr(y)
While yy.Length < 8
yy = "0" + yy
End While
For c As Integer = 0 To 7
If xx.Chars(c) = "1" Or yy.Chars(c) = "1" Then
OrIt += "1"
Else
OrIt += "0"
End If
Next
End Function
Private Function ToBinary(ByVal x As Long) As String
Dim temp As String = ""
Do
If x Mod 2 Then
temp = "1" + temp
Else
temp = "0" + temp
End If
x = x \ 2
If x < 1 Then Exit Do
Loop
While temp.Length < 8
temp = "0" + temp
End While
Return temp
End Function
Private Function ToInteger(ByVal x As Long) As String
Dim temp As String
Dim ch As Char
Dim multiply As Integer = 1
Dim subtract As Integer = 1
Dim len As Integer = CStr(x).Length
For Each ch In CStr(x)
For len = 1 To CStr(x).Length - subtract
multiply = multiply * 2
Next
multiply = CInt(ch.ToString) * multiply
temp = multiply + temp
subtract = subtract + 1
multiply = 1
Next
Return temp
End Function
Private Sub btnWakeUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim udpClient As New UdpClient
Dim buf(101) As Char
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(buf)
For x As Integer = 0 To 5
sendBytes(x) = CInt("&HFF")
Next
Dim MacAddress As String
MacAddress = Replace(edtMac.Text, "-", "")
Dim i As Integer = 6
For x As Integer = 1 To 16
sendBytes(i) = CInt("&H" + MacAddress.Substring(0, 2))
sendBytes(i + 1) = CInt("&H" + MacAddress.Substring(2, 2))
sendBytes(i + 2) = CInt("&H" + MacAddress.Substring(4, 2))
sendBytes(i + 3) = CInt("&H" + MacAddress.Substring(6, 2))
sendBytes(i + 4) = CInt("&H" + MacAddress.Substring(8, 2))
sendBytes(i + 5) = CInt("&H" + MacAddress.Substring(10, 2))
i += 6
Next
Dim myAddress As String
'' Split user IP address
Dim myIpArray() As String
Dim a, b, c, d As Int64
myIpArray = edtIpAddress.Text.Split(".")
For i = 0 To myIpArray.GetUpperBound(0)
Select Case i
Case Is = 0
a = Convert.ToInt64(myIpArray(i))
Case Is = 1
b = Convert.ToInt64(myIpArray(i))
Case Is = 2
c = Convert.ToInt64(myIpArray(i))
Case Is = 3
d = Convert.ToInt64(myIpArray(i))
End Select
Next
Dim mySubnetArray() As String
Dim sm1, sm2, sm3, sm4 As Int64
mySubnetArray = edtSubnetMask.Text.Split(".")
For i = 0 To mySubnetArray.GetUpperBound(0)
Select Case i
Case Is = 0
sm1 = Convert.ToInt64(mySubnetArray(i))
Case Is = 1
sm2 = Convert.ToInt64(mySubnetArray(i))
Case Is = 2
sm3 = Convert.ToInt64(mySubnetArray(i))
Case Is = 3
sm4 = Convert.ToInt64(mySubnetArray(i))
End Select
Next
myAddress = ToInteger(OrIt(ToBinary(a), InvertBinary(ToBinary(sm1)))) & "." & ToInteger(OrIt(ToBinary(b), InvertBinary(ToBinary(sm2)))) & _
"." & ToInteger(OrIt(ToBinary(c), InvertBinary(ToBinary(sm3)))) & "." & ToInteger(OrIt(ToBinary(d), InvertBinary(ToBinary(sm4))))
udpClient.Send(sendBytes, sendBytes.Length, myAddress, CInt(edtPortNo.Text))
lblSent.Text = " Magic Packet sent to " & myAddress
End Sub
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>www.depicus.com</title>
</head>
<body>
<form id=myForm method=post runat="server">
<asp:textbox id=edtMac runat="server" CssClass="myInput">00-90-27-A3-22-FE</asp:TextBox><br>
<asp:textbox id=edtIpAddress runat="server" MaxLength="15" CssClass="myInput">217.204.255.55</asp:TextBox><br>
<asp:textbox id=edtSubnetMask runat="server" MaxLength="15" CssClass="myInput">255.255.255.240</asp:TextBox><br>
<asp:textbox id=edtPortNo runat="server" MaxLength="5" CssClass="myInput">7</asp:TextBox><br>
<asp:button id=btnWakeUp runat="server" onclick="btnWakeUp_Click" Text="Wake On Wan" CssClass="bluebutton"></asp:Button>
<asp:Label id=lblSent runat="server" CssClass="errortext"><p> </p></asp:Label>
</form>
</body>
</html>
|