Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Nevyn's SimpleWebServer Translated to VB


  • Please log in to reply
27 replies to this topic

#1 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 21 August 2011 - 05:46 AM

I am attaching a VB version of the Simple Web Server referenced here, http://forums.netdui...-netduino-plus/ The original post explains the program. I have tested the VB version with the implemented commands, bad commands and the interrupt button. Everything seems to work OK. I ran into a couple of problems parsing the request string in Sub ProcessRequest(client As Socket, request As String). The first had to do with newline characters not being present in the request string. This may be due to the VB vs C# implementations. The second problem was related to a spurious request by IE9 for GET /favicon.ico HTTP/1.1 even though I issued, http://192.168.0.111....html?SayHello. Apparently, this is a tiny icon placed in the address bar. In any case, the program needed some code to ignore it. This is a nice program; thank you Nevyn, but I do hate deeply nested curly brackets when porting code. Baxter

Attached Files



#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 21 August 2011 - 01:48 PM

The second problem was related to a spurious request by IE9 for GET /favicon.ico HTTP/1.1 even though I issued, http://192.168.0.111....html?SayHello. Apparently, this is a tiny icon placed in the address bar. In any case, the program needed some code to ignore it.

Thanks for the note - I'll make a modification to the C# implementation to take this into consideration.

This is a nice program; thank you Nevyn, but I do hate deeply nested curly brackets when porting code.

I always have a rule that I don't write code which does not fit on one screen width - I've been working on full HD monitors for the last three years so one screen is a lot of code.

Glad you found it useful - I'll drop a link to your code from my blog.

Regards,

Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 11 September 2011 - 09:00 AM

I am attaching a VB version of the Simple Web Server referenced here,

I'm adding this project to Codeplex - am I OK to add the VB code to the project? I going to try and bring the web server code and the Silverlight code together in one place.

Alternatively, if you want I can add you as a contributor if you have a Codeplex ID. PM me your details if interested.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 11 September 2011 - 01:39 PM

I'd love to see the VB code on Codeplex. I'd say "just add it", it's your code. ;) I've translated your code myself and found that the "NewLine" hack is not needed anymore. I've attached my take on the translation. :)

Attached Files



#5 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 11 September 2011 - 06:39 PM

Hi Nevyn, Post it wherever you want; It's your code. I have added some further command functionality: (1) NIST Daytime protocol client class to get and set the clock through the browser and, (2) SD card file manager class (volume info, list files/folders on card, make/delete subdirectory, read/write files, clean SD, etc) I will post these to the forum within the next a few days. Baxter

#6 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 14 December 2011 - 02:33 PM

Hi. I just installed the VB Webserver and it I am able to communicate with it. It is throwing errors because I have no SD card but, at least everything else is working. I am going to buy a mini-SD card today. Some questions: 1. I noticed in the webserver code that the SD is not mounted anywhere. Does System.IO.File.Exists(fullPath) automatically know to check an SD card? 2. How do I write files to the SD? Does it show up as a removable drive when installed or do I need to attach it to my PC, load files and then attach it to the NetDuino? Thanks! terry

#7 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 14 December 2011 - 04:22 PM

1. I noticed in the webserver code that the SD is not mounted anywhere. Does System.IO.File.Exists(fullPath) automatically know to check an SD card?

2. How do I write files to the SD? Does it show up as a removable drive when installed or do I need to attach it to my PC, load files and then attach it to the NetDuino?

To put files on the SD card you have two alternatives, one is to remove the SD card and put it in a PC and write to it from the PC. The second alternative is to modify the web server to allow files to be written to the SD card by the web server code itself. It is trivial to do just be aware of the implications.

I never had to make any modifications to the system to make File.Exists check the SD card.

Hope this helps,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#8 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 14 December 2011 - 04:48 PM

It is trivial to do just be aware of the implications.



Thanks Mark. What are the implications of doing it in code? Just bloat?

#9 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 14 December 2011 - 05:37 PM

Thanks Mark. What are the implications of doing it in code? Just bloat?

I was thinking more about security.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#10 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 14 December 2011 - 06:46 PM

Thanks Mark. What are the implications of doing it in code? Just bloat?



Understood Mark.

I just installed an SD card and put a "Default.htm" file into a www directory.

The code throws an error when looking to see if the file exists.

Step into: Stepping over non-user code 'System.IO.File.Exists'
A first chance exception of type 'System.IO.IOException' occurred in Microsoft.SPOT.IO.dll
Step into: Stepping over non-user code 'System.IO.File.Exists'

Any Ideas?

#11 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 14 December 2011 - 07:03 PM

I just noticed that I get about 50 errors whenever I try System.IO.File.Exists. The errors happen regardless of having the SD card in or not. I tried a simple program where System.IO.File.Exists('@\SD\www\Default.htm') was the only line and I get the errors.

#12 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 14 December 2011 - 07:37 PM

I just noticed that I get about 50 errors whenever I try System.IO.File.Exists.

The errors happen regardless of having the SD card in or not.


I tried a simple program where System.IO.File.Exists('@\SD\www\Default.htm') was the only line and I get the errors.

Just wondering about the card / capacity. If memory serves 4GB are supported with some larger cards in the later version of the framework. What card make, capacity and format are you using?

Have you tried a simple program to read or write a file?

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#13 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 14 December 2011 - 07:46 PM

Just wondering about the card / capacity. If memory serves 4GB are supported with some larger cards in the later version of the framework. What card make, capacity and format are you using?

Have you tried a simple program to read or write a file?

Regards,
Mark



Hi Mark.

I moved the question to a new thread because I didn't want this one to lose focus on the webserver... I will put all the SD card info there.

Thanks again,
terry

#14 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 15 December 2011 - 12:55 AM

Hi Nevyn,

Post it wherever you want; It's your code. I have added some further command functionality:

(1) NIST Daytime protocol client class to get and set the clock through the browser and,
(2) SD card file manager class (volume info, list files/folders on card, make/delete subdirectory, read/write files, clean SD, etc)

I will post these to the forum within the next a few days.

Baxter


Hi Baxter. Can you post your code for setting the time through the browser?

Thanks!
Terry

#15 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 15 December 2011 - 05:12 PM

Does anyone know if the socket class can be made to have events? Instead of "Listening" in a loop?

#16 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 16 December 2011 - 05:30 AM

Hi Terry,

Sure, I will post the code, but first I need to do some cleanup. I haven't had too much time to work on this project lately. The code is in the "first make it work" state. Basically what I set out to do was build a manager for the Netduino with enhanced functionality for the web server. I got sick of unplugging the SD card to transfer a file so I put together a PC client that issues url-encoded commands through the VB web browser control. It uploads and downloads files and data using the WebClient class. So then I had to write a more robust request parser for the web server and so on ...

I don't know anything about an event driven web server, but there is a lot of good VB socket programming stuff here,

http://vbex.ru/index.../Socket_Network

Back to your question, this is not the full project code, but the daytime server will fit into Nevyn's original process command method by simply adding an entry into the ProcessCommand Select Case structure.
Case "gettime"
    Send(client, "Time from Netduino: " & DateTime.Now.ToString)

Case "settime"    
    Using NIST_Time As DaytimeNIST = New DaytimeNIST("wwv.nist.gov", -8)
       NIST_Time.getTimeNIST()
       Dim newTime As String = DateTime.Now.ToString
       Send(client, "<html><body><p>" & "Set Time " & newTime & "</p></body></html>")
    End Using
------------------------------------------------------------------

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoPlus
Imports System.Threading
Imports System.Net
Imports System.Text
Imports System.Net.Sockets
Imports Microsoft.SPOT.Net.NetworkInformation

''' <summary>
''' Set Netduino Plus time using NIST Daytime Protocol
''' Upon return from getTimeNIST, the clock is running
''' with the correct correction for daylight savings or
''' standard time and the input UTC offset.
''' </summary>
''' <remarks></remarks>
Public Class DaytimeNIST
    Implements IDisposable
    'Private disposed As Boolean = False

    'these servers are always reliable for Daytime protocol
    Private servers() As String = {"wwv.nist.gov", "utcnist2.colorado.edu", "utcnist.colorado.edu"}
    Private sIndex As Integer = 0 'server index
    Private buffer(51) As Byte 'Size of Daytime Protocol response

    Public Sub New()
        Me.TimeServer = servers(sIndex)
        sIndex += 1
    End Sub

    Public Sub New(ByVal tServer As String, ByVal offsetUTC As Integer)
        Me.TimeServer = tServer.Trim
        Me.UTC_offset = offsetUTC
    End Sub

    Public Property TimeServer As String
    Public Property UTC_offset As Integer = -8 'Default: Pacific Time (US & Canada)
    Public Property successful As Boolean = False
    Public Property networkDateTime As String

    Public Sub getTimeNIST()
        'format of Daytime string: JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM
        Dim nBytes As Integer = 0
        Dim count As Integer = 0
        Dim stdTime As Integer = 0
        Dim s1() As String = {}
        Dim s2() As String = {}
        Dim s3() As String = {}
        Dim success As Boolean = False
        ' Connect to timeserver 
        Do While (nBytes = 0)
            count += 1
            Using s As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                Dim ep As IPEndPoint = New IPEndPoint(Dns.GetHostEntry(Me.TimeServer).AddressList(0), 13)
                s.Connect(ep)
                nBytes = s.Receive(buffer)
                s.Close()
            End Using
            If (nBytes <> 0) Then
                Dim resp As String = Encoding.UTF8.GetChars(buffer)
                s1 = resp.Split(" "c)
                s2 = s1(1).Split("-"c) 'yr, mo, day
                s3 = s1(2).Split(":"c) 'hr, min, sec
                Dim dst As Integer = CInt(s1(3))
                'Set the correct daylight or standard time
                If (dst = 0 Or dst > 51) Then 'on standard time
                    stdTime = 0
                ElseIf (dst <= 50 And dst > 1) Then
                    stdTime = 1
                End If

                Dim dt As DateTime = New DateTime(2000 + CInt(s2(0)), CInt(s2(1)), CInt(s2(2)),
                                          CInt(s3(0)), CInt(s3(1)), CInt(s3(2))).AddHours(Me.UTC_offset + stdTime)
                Utility.SetLocalTime(dt)
                Me.networkDateTime = resp
                Me.successful = True
                Exit Do
            End If
            Debug.Print("count = " & count.ToString)
            If (count = 3) Then
                If (sIndex <= 2) Then
                    Me.TimeServer = servers(sIndex)
                    sIndex += 1 'point to next server
                    Debug.Print("Trying a different server : " & Me.TimeServer)
                    count = 0
                Else
                    Debug.Print("tried 3 servers something is wrong")
                    Exit Do
                End If
            End If
            Thread.Sleep(5000) 'per NIST instructions do not query more than once every 4 sec
        Loop

    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
                Debug.Print("disposing Daytime Client ")
                Debug.Print("GC: " & Debug.GC(True).ToString)
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
Baxter

#17 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 16 December 2011 - 02:19 PM

Thanks Baxter! I will try adding that today. The reason I was asking about the event driven webserver is because mine (yours and Moskus') hangs after a couple of hours. I am not sure if this is a hardware or software issue. Thanks again. terry

#18 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 16 December 2011 - 03:34 PM

I created a timer that fires in the mainprogram every 10 minutes -- it reboots the server.

_MyTimerReset = New Timer(AddressOf MyTimerReset_Tick, Nothing, 1000 * 60 * 10, 1000 * 60 * 10)


Private Sub MyTimerReset_Tick(O As Object)
        PowerState.RebootDevice(False)
    End Sub

I found out the hard way that if you have 0 for the timer 'due time' (third variable in the new statement) -- you will brick the netduino since it is continually trying to reboot! The good news is that this forced me to update the TinyBootDecompressor :lol:

#19 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 16 December 2011 - 08:13 PM

Hi Terry,

When I first started with this server it would go down because of spurious requests. One in particular was the favicon that was coming from IE Explorer. Other times a request would be just nothing which I think was coming from the router. I had some network problems so I started switching out routers and then I started getting requests like:

GET /HNAP1/ HTTP/1.1

GET /TEADevInfo/ HTTP/1.1

I finally tracked this down to get the following information.

Quote:

"HNAP = Home Network Administration Protocol
TEADevInfo = a firmware file on DLink routers

The source of the requests is a program called "Network Magic" which is bundled with DLink routers. Apparently, this program scans all IP addresses on the LAN, looking for DLink routers."

N.B. I think this also happens with Cisco software. Search for HNAP Protocol

I kept adding traps in the While-Listen loop, but then it dawned on me to add a try-catch block within the Listen loop and eat the exception.

While (True)
    Try
       Using client As Socket = _socket.Accept()

----
----

       End Using
    Catch ex As SocketException
        Debug.Print(ex.StackTrace)
    End Try
End While

This seemed to solve the problem of the server going down because of bad requests. YMMV

Baxter

#20 seascan

seascan

    Advanced Member

  • Members
  • PipPipPip
  • 88 posts

Posted 16 December 2011 - 09:18 PM

Hi Terry,

When I first started with this server it would go down because of spurious requests. One in particular was the favicon that was coming from IE Explorer. Other times a request would be just nothing which I think was coming from the router. I had some network problems so I started switching out routers and then I started getting requests like:

GET /HNAP1/ HTTP/1.1

GET /TEADevInfo/ HTTP/1.1

I finally tracked this down to get the following information.

Quote:

"HNAP = Home Network Administration Protocol
TEADevInfo = a firmware file on DLink routers

The source of the requests is a program called "Network Magic" which is bundled with DLink routers. Apparently, this program scans all IP addresses on the LAN, looking for DLink routers."

N.B. I think this also happens with Cisco software. Search for HNAP Protocol

I kept adding traps in the While-Listen loop, but then it dawned on me to add a try-catch block within the Listen loop and eat the exception.

While (True)
    Try
       Using client As Socket = _socket.Accept()

----
----

       End Using
    Catch ex As SocketException
        Debug.Print(ex.StackTrace)
    End Try
End While

This seemed to solve the problem of the server going down because of bad requests. YMMV

Baxter


Great Idea. I just added the TRY/CATCH.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.