I have been trying to build a html server using a raptor, but have not had any luck. I have tried translating several sets of example code to VB without luck. This code is the only adapted code examples I have been able to run. First the code throws the NetworkDown event, although the cat5 cable is connected, and shows traffic (the LED blinks).
The debug shows that the IP address remains 0.0.0.0.
The idea of using the Premium.Net package is appealing as it should detect cable problems. DHCP is also important for my application. Any ideas of why this does not work are appreciated.
And I would also appreciate knowing why when I copy code from Visual Studio and paste into the forum composer, I get an extra blank line for every code line.
Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Gadgeteer.Modules.Seeed
Imports Microsoft.SPOT.Hardware
Imports GHI.Hardware.G400
Imports System
Imports System.Net
Imports Microsoft.SPOT
Imports GHI.Premium.Net
Imports System.Text
Imports System.Threading
Imports Microsoft.SPOT.Net.NetworkInformation
Imports System.Net.Sockets
Namespace GadgeteerApp1
Partial Public Class Program
'Set iface to the first available network interface
Private Shared WithEvents iface As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces(0)
Private Shared server As Socket
Dim WithEvents InternetTimer As GT.Timer = New GT.Timer(1000)
Public Sub ProgramStarted()
Debug.Print("Program Started")
Dim rtc_raptor As New RTC_Raptor
iface.EnableDhcp()
iface.EnableDynamicDns()
Debug.Print(iface.PhysicalAddress.ToString())
Debug.Print(iface.GatewayAddress.ToString)
InternetTimer.Start()
End Sub
Private listening As Boolean = False
Private Sub InternetTimer_Tick(timer As Gadgeteer.Timer) Handles InternetTimer.Tick
If listening = False Then
If iface.IPAddress = "0.0.0.0" Then
Debug.Print("DHCP=" & iface.IsDhcpEnabled.ToString & ", but no connection")
Exit Sub
Else
Debug.Print("Has IP address of " & iface.IPAddress.ToString)
End If
server = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'Initialize a local endpoint to listen on the default http port 80
Dim localEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 80)
server.Bind(localEndPoint)
server.Listen(1)
Else
End If
Dim clientSocket As Socket = server.Accept ' wait for client to accept
' Process the client request.
ProcessClientRequest(clientSocket)
End Sub
Private Shared Sub ProcessClientRequest(ByVal m_clientSocket As Socket)
Const c_microsecondsPerSecond As Int32 = 1000000
' 'using' ensures that the client's socket gets closed.
' Wait for the client request to start to arrive.
Using m_clientSocket
Dim buffer() As Byte = New Byte((1024) - 1) {}
If m_clientSocket.Poll((5 * c_microsecondsPerSecond), SelectMode.SelectRead) Then
' If 0 bytes in buffer,
' then the connection has been closed,
' reset, or terminated.
If (m_clientSocket.Available = 0) Then
Return
End If
' Read the first chunk of the request
' (we don't actually do anything with it).
Dim bytesRead As Int32 = m_clientSocket.Receive(buffer, m_clientSocket.Available, SocketFlags.None)
' Return a static HTML document to the client.
Dim s As String = ("HTTP/1.1 200 OK" & vbCrLf & "Content-Type: text/html; charset=utf-8" & vbCrLf & vbCrLf & "<html><head><title>.NET Micro Framework We" & _
"b Server</title></head>" + "<body><bold><a href=\""http://www.ghielectronics.com/\"">Learn more about the .NET Micro Framework with" & _
" FEZ by clicking here</a></bold></body></html>")
Dim buf() As Byte = System.Text.Encoding.UTF8.GetBytes(s)
Dim offset As Integer = 0
Dim ret As Integer = 0
Dim len As Integer = buf.Length
While (len > 0)
ret = m_clientSocket.Send(buf, offset, len, SocketFlags.None)
len = (len - ret)
offset = (offset + ret)
End While
m_clientSocket.Close()
End If
End Using
End Sub
Private Sub ethernet_ENC28_NetworkDown(sender As Gadgeteer.Modules.Module.NetworkModule, state As Gadgeteer.Modules.Module.NetworkModule.NetworkState) Handles ethernet_ENC28.NetworkDown
Debug.Print("network down fired")
End Sub
Private Sub ethernet_ENC28_NetworkUp(sender As Gadgeteer.Modules.Module.NetworkModule, state As Gadgeteer.Modules.Module.NetworkModule.NetworkState) Handles ethernet_ENC28.NetworkUp
Debug.Print("Network up fired")
End Sub
End Class
End Namespace