LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-05-2006, 09:02 AM   #1
Maverick1182
Member
 
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Rep: Reputation: 30
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>&nbsp;</p></asp:Label>

</form>



</body>



</html>
 
Old 12-06-2006, 04:39 PM   #2
hob
Senior Member
 
Registered: Mar 2004
Location: Wales, UK
Distribution: Debian, Ubuntu
Posts: 1,075

Rep: Reputation: 45
IIRC the current version of Mono supports C#, but not VB.NET, so you will need to convert this code into C# in order to run it on a Linux server.
 
Old 12-06-2006, 06:16 PM   #3
Maverick1182
Member
 
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Original Poster
Rep: Reputation: 30
Okay, I will have a word withsomeone on the conversion. Thanks for the direction
 
Old 12-06-2006, 06:52 PM   #4
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
I would try to use code behind as much as possible instead of embedding the code in a webpage.
 
Old 12-07-2006, 05:33 AM   #5
Maverick1182
Member
 
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Original Poster
Rep: Reputation: 30
Do you mean I can somehow create a standalone application which can do the same thing?
 
Old 12-07-2006, 07:27 PM   #6
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
Yes and separate the vb.net code from the html server code. Look at the .Net quick start. If I remember it just and include of the vb.net code at the top of the aspx page.
Good Luck!
 
  


Reply

Tags
mono


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to code a program to xfer data through the net JonBrant Programming 3 10-20-2006 08:16 PM
LXer: Easy step-by-step guide for incorporating SVG into Web pages LXer Syndicated Linux News 0 07-29-2006 01:54 PM
Where can i find a sample source code using Net::NNTP? repolona Linux - Newbie 1 05-27-2006 04:07 PM
LXer: Attack code for old Firefox bug hits the net LXer Syndicated Linux News 0 12-14-2005 02:16 PM
is there any tool avilable to convert java/sql code into asp.net/sql2000 rddreamz Programming 1 11-08-2004 03:16 PM


All times are GMT -5. The time now is 02:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration