Hi t all, i'm new to this forum. I start my first project with netduino. I try to build a simple HTTP web server with VB.net.
The web server is work but sometime only with Chrome Browser and IE it doest not respond with HTTP RESPONSE to browser GET request.
With safari or firefox works great. I paste my code:
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] Microsoft.SPOT
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] System.Net.Sockets
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] System.Net
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] System.Threading
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] System.Text
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] Microsoft.SPOT.Hardware
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] SecretLabs.NETMF.Hardware
- [color=rgb(0,0,255);font-weight:bold;]Imports[/color] SecretLabs.NETMF.Hardware.Netduino
-
- [color=rgb(0,0,255);font-weight:bold;]Namespace[/color] LogConsultingWebServer
- [color=rgb(0,0,255);font-weight:bold;]Public[/color] [color=rgb(0,0,255);font-weight:bold;]Class[/color] WebServer
- [color=rgb(0,128,0);font-style:italic;]'Implements IDisposable[/color]
- [color=rgb(0,0,255);font-weight:bold;]Private[/color] listeningSocket [color=rgb(0,0,255);font-weight:bold;]As[/color] Socket = [color=rgb(0,0,255);font-weight:bold;]Nothing[/color]
- [color=rgb(0,128,0);font-style:italic;]'open connection to onbaord led so we can blink it with every request[/color]
-
-
- [color=rgb(0,0,255);font-weight:bold;]Public[/color] [color=rgb(0,0,255);font-weight:bold;]Sub[/color] [color=rgb(0,0,255);font-weight:bold;]New[/color]()
- [color=rgb(0,128,0);font-style:italic;]'Initialize Socket class[/color]
- listeningSocket = [color=rgb(0,0,255);font-weight:bold;]New[/color] Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
- [color=rgb(0,128,0);font-style:italic;]'Request and bind to an IP from DHCP server[/color]
- listeningSocket.Bind([color=rgb(0,0,255);font-weight:bold;]New[/color] IPEndPoint(IPAddress.Any, [color=rgb(165,42,42);]80[/color]))
- [color=rgb(0,128,0);font-style:italic;]'Debug print our IP address[/color]
- Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()([color=rgb(165,42,42);]0[/color]).IPAddress)
- [color=rgb(0,128,0);font-style:italic;]'Start listen for web requests[/color]
- listeningSocket.Listen([color=rgb(165,42,42);]10[/color])
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Sub[/color]
-
- [color=rgb(0,0,255);font-weight:bold;]Public[/color] [color=rgb(0,0,255);font-weight:bold;]Sub[/color] ListenForRequest()
- [color=rgb(0,0,255);font-weight:bold;]Try[/color]
- [color=rgb(0,0,255);font-weight:bold;]While[/color] [color=rgb(0,0,255);font-weight:bold;]True[/color]
- [color=rgb(0,0,255);font-weight:bold;]Using[/color] clientSocket [color=rgb(0,0,255);font-weight:bold;]As[/color] Socket = listeningSocket.Accept()
- [color=rgb(0,128,0);font-style:italic;]'Get clients IP[/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] clientIP [color=rgb(0,0,255);font-weight:bold;]As[/color] IPEndPoint = [color=rgb(0,0,255);font-weight:bold;]TryCast[/color](clientSocket.RemoteEndPoint, IPEndPoint)
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] clientEndPoint [color=rgb(0,0,255);font-weight:bold;]As[/color] EndPoint = clientSocket.RemoteEndPoint
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] bytesReceived [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Integer[/color] = clientSocket.Available
- [color=rgb(0,0,255);font-weight:bold;]If[/color] bytesReceived > [color=rgb(165,42,42);]0[/color] [color=rgb(0,0,255);font-weight:bold;]Then[/color]
- [color=rgb(0,128,0);font-style:italic;]'Get request[/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] buffer [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Byte[/color]() = [color=rgb(0,0,255);font-weight:bold;]New[/color] [color=rgb(106,90,205);]Byte[/color](bytesReceived - [color=rgb(165,42,42);]1[/color]) {}
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] byteCount [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Integer[/color] = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None)
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] request [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(0,0,255);font-weight:bold;]New[/color] [color=rgb(106,90,205);]String[/color](Encoding.UTF8.GetChars(buffer))
- Debug.Print(request)
- [color=rgb(0,128,0);font-style:italic;]'Compose a response [/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] response [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]String[/color] = [color=rgb(165,42,42);]""[/color]
- i.SwitchState()
- [color=rgb(0,0,255);font-weight:bold;]Select[/color] [color=rgb(0,0,255);font-weight:bold;]Case[/color] i.GetState
- [color=rgb(0,0,255);font-weight:bold;]Case[/color] [color=rgb(0,0,255);font-weight:bold;]False[/color]
- response = c.InterruttoreApertoStream
- [color=rgb(0,0,255);font-weight:bold;]Case[/color] [color=rgb(0,0,255);font-weight:bold;]True[/color]
- response = c.InterruttoreChiusoStream
-
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Select[/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] header [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]String[/color] = [color=rgb(165,42,42);]""[/color]
-
-
-
-
- header = [color=rgb(165,42,42);]"HTTP/1.1 200 OK"[/color] & Constants.vbCr & Constants.vbLf & [color=rgb(165,42,42);]"Server: Netduino"[/color] & Constants.vbCr & Constants.vbLf & [color=rgb(165,42,42);]"Pragma : no-cache"[/color] & Constants.vbCr & Constants.vbLf &[color=rgb(165,42,42);]"Content-Type: text/html;charset=UTF-8"[/color] & Constants.vbCr & Constants.vbLf & [color=rgb(165,42,42);]"Cache-Control: no-cache"[/color] &Constants.vbCr & Constants.vbLf & [color=rgb(165,42,42);]"Content-Length: "[/color] & response.Length.ToString() & Constants.vbCr &Constants.vbLf & [color=rgb(165,42,42);]"Connection: close"[/color] & Constants.vbCr & Constants.vbLf & Constants.vbCr & Constants.vbLf
-
-
-
-
- Debug.Print(header & Constants.vbCrLf & response)
- clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None)
- SendData(clientSocket, Encoding.UTF8.GetBytes(response))
-
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]If[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Using[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]While[/color]
- [color=rgb(0,0,255);font-weight:bold;]Catch[/color] ex [color=rgb(0,0,255);font-weight:bold;]As[/color] Exception
- Debug.Print(ex.ToString)
-
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Try[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Sub[/color]
-
- [color=rgb(0,0,255);font-weight:bold;]Protected[/color] [color=rgb(0,0,255);font-weight:bold;]Function[/color] SocketConnected(s [color=rgb(0,0,255);font-weight:bold;]As[/color] Socket) [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Boolean[/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] part1 [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Boolean[/color] = s.Poll([color=rgb(165,42,42);]10[/color], SelectMode.SelectRead)
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] part2 [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Boolean[/color] = (s.Available = [color=rgb(165,42,42);]0[/color])
- [color=rgb(0,0,255);font-weight:bold;]If[/color] part1 [color=rgb(0,0,255);font-weight:bold;]And[/color] part2 [color=rgb(0,0,255);font-weight:bold;]Then[/color]
- [color=rgb(0,0,255);font-weight:bold;]Return[/color] [color=rgb(0,0,255);font-weight:bold;]False[/color]
- [color=rgb(0,0,255);font-weight:bold;]Else[/color]
- [color=rgb(0,0,255);font-weight:bold;]Return[/color] [color=rgb(0,0,255);font-weight:bold;]True[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]If[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Function[/color]
-
- [color=rgb(0,0,255);font-weight:bold;]Protected[/color] [color=rgb(0,0,255);font-weight:bold;]Function[/color] SendData(client [color=rgb(0,0,255);font-weight:bold;]As[/color] Socket, data [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Byte[/color]()) [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Integer[/color]
- [color=rgb(0,0,255);font-weight:bold;]Dim[/color] ret [color=rgb(0,0,255);font-weight:bold;]As[/color] [color=rgb(106,90,205);]Integer[/color] = [color=rgb(165,42,42);]0[/color]
- [color=rgb(0,0,255);font-weight:bold;]Try[/color]
- [color=rgb(0,0,255);font-weight:bold;]If[/color] SocketConnected(client) [color=rgb(0,0,255);font-weight:bold;]Then[/color]
- ret = client.Send(data, data.Length, SocketFlags.None)
- [color=rgb(0,0,255);font-weight:bold;]Else[/color]
- client.[color=rgb(0,0,102);]Close[/color]()
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]If[/color]
- [color=rgb(0,0,255);font-weight:bold;]Catch[/color] ex [color=rgb(0,0,255);font-weight:bold;]As[/color] Exception
- Debug.Print([color=rgb(165,42,42);]"Error on sending data to client / Closing Client"[/color])
- [color=rgb(0,0,255);font-weight:bold;]Try[/color]
- client.[color=rgb(0,0,102);]Close[/color]()
- [color=rgb(0,0,255);font-weight:bold;]Catch[/color] ex2 [color=rgb(0,0,255);font-weight:bold;]As[/color] Exception
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Try[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Try[/color]
-
- [color=rgb(0,0,255);font-weight:bold;]Return[/color] ret
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Function[/color]
- [color=rgb(0,128,0);font-style:italic;]'#Region "IDisposable Members"[/color]
- [color=rgb(0,128,0);font-style:italic;]' Protected Overrides Sub Finalize()[/color]
- [color=rgb(0,128,0);font-style:italic;]' Try[/color]
- [color=rgb(0,128,0);font-style:italic;]' Dispose()[/color]
- [color=rgb(0,128,0);font-style:italic;]' Finally[/color]
- [color=rgb(0,128,0);font-style:italic;]' MyBase.Finalize()[/color]
- [color=rgb(0,128,0);font-style:italic;]' End Try[/color]
- [color=rgb(0,128,0);font-style:italic;]' End Sub[/color]
- [color=rgb(0,128,0);font-style:italic;]' Public Sub Dispose() Implements IDisposable.Dispose[/color]
- [color=rgb(0,128,0);font-style:italic;]' If listeningSocket IsNot Nothing Then[/color]
- [color=rgb(0,128,0);font-style:italic;]' listeningSocket.Close()[/color]
- [color=rgb(0,128,0);font-style:italic;]' End If[/color]
- [color=rgb(0,128,0);font-style:italic;]' End Sub[/color]
- [color=rgb(0,128,0);font-style:italic;]'#End Region[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Class[/color]
- [color=rgb(0,0,255);font-weight:bold;]End[/color] [color=rgb(0,0,255);font-weight:bold;]Namespace[/color]
[font="arial, helvetica, sans-serif;"][color=rgb(0,0,255);font-weight:bold;]May be i wrong to write the HTTP response???[/color][/font]
[font="arial, helvetica, sans-serif;"][color=rgb(0,0,255);font-weight:bold;]Can somone help me?[/color][/font]