Web 服务器 Windows 10 物联网 (vb.net)

Web Server with Windows 10 IoT (vb.net)

我正在尝试从 c# 中的各种来源导入和转换以创建 Web 服务器。配置是这样的:

配置

Development platform: VS Community in 2015

Type of application: VB / Windows IoT Core / Background Application

Hardware: Raspberry Pi 3

OS: Windows 10 IoT (v. 10.0.14393.693)


问题

在某些情况下无法发送页面输出。但是在任何情况下(即使它显示输出),页面都会继续加载直到应用程序崩溃或陷入无限循环。

代码

任务

Public NotInheritable Class StartupTask
    Implements IBackgroundTask
    Private _Deferral As BackgroundTaskDeferral
    Public Async Sub Run(taskInstance As IBackgroundTaskInstance) Implements IBackgroundTask.Run
        taskInstance.GetDeferral()
        Dim server As New webServer()
        Await Windows.System.Threading.ThreadPool.RunAsync(AddressOf server.Start)
    End Sub
End Class

webserverclass

Imports System.Text
Imports Windows.Networking.Sockets
Imports Windows.Storage.Streams

Friend Class webServer

    Private Const BufferSize As UInteger = 8192
    Public Async Sub Start()
        Try
            Dim listener As New StreamSocketListener()
            Debug.WriteLine("Debug: 1.1")
            Await listener.BindServiceNameAsync("8081").AsTask()
            Debug.WriteLine("Debug: 1.2")
            AddHandler listener.ConnectionReceived, Async Sub(sender, args)

                                                        Debug.WriteLine("Debug: 2")

                                                        Dim request As New StringBuilder()
                                                        Using input As IInputStream = args.Socket.InputStream
                                                            Dim data As Byte() = New Byte(BufferSize - 1) {}
                                                            Dim buffer As IBuffer = data.AsBuffer()
                                                            Dim dataRead As UInteger = BufferSize
                                                            While dataRead = BufferSize
                                                                Await input.ReadAsync(buffer, BufferSize, InputStreamOptions.[Partial])
                                                                request.Append(Encoding.UTF8.GetString(data, 0, data.Length))
                                                                dataRead = buffer.Length

                                                                Debug.WriteLine("Debug: 2.2 - " & request.ToString)

                                                            End While
                                                        End Using

                                                        Debug.WriteLine("Debug: 3")

                                                        Using output As IOutputStream = args.Socket.OutputStream

                                                            Debug.WriteLine("Debug: 3.1")

                                                            Using response As Stream = output.AsStreamForWrite()

                                                                Debug.WriteLine("Debug: 3.2")

                                                                Dim page As String = ""
                                                                Dim folder = Package.Current.InstalledLocation
                                                                Dim file = Await folder.GetFileAsync("index.html")
                                                                Dim readFile = Await Windows.Storage.FileIO.ReadLinesAsync(file)
                                                                For Each line In readFile
                                                                    page += line
                                                                Next

                                                                Debug.WriteLine("Debug: 3.3 - " & page)

                                                                Dim bodyArray As Byte() = Encoding.UTF8.GetBytes(page)
                                                                Dim bodyStream = New MemoryStream(bodyArray)

                                                                Debug.WriteLine("Debug: 3.4")

                                                                Dim header = "HTTP/1.1 200 OK" & vbCr & vbLf + "Content-Length: {bodyStream.Length}" & vbCr & vbLf + "Connection: close" & vbCr & vbLf & vbCr & vbLf
                                                                Dim headerArray As Byte() = Encoding.UTF8.GetBytes(header)

                                                                Debug.WriteLine("Debug: 3.5")

                                                                Await response.WriteAsync(headerArray, 0, headerArray.Length)

                                                                Debug.WriteLine("Debug: 3.6")

                                                                Await bodyStream.CopyToAsync(response)

                                                                Debug.WriteLine("Debug: 3.7")

                                                                Await response.FlushAsync()

                                                                Debug.WriteLine("Debug: 3.8")

                                                            End Using
                                                        End Using

                                                        Debug.WriteLine("Debug: 4")

                                                    End Sub

            Debug.WriteLine("Debug: 1.3")

        Catch generatedExceptionName As Exception
            Debug.WriteLine("Error: " & generatedExceptionName.Message)
        End Try
    End Sub



End Class

清单

  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
  </Capabilities>

调试输出

Debug: 1.1

Debug: 1.2

Debug: 1.3

Debug: 2

Debug: 2.2 - GET /index.html HTTP/1.1 Host: 192.168.1.146:8081 Connection: keep-alive (and all the header...)

Debug: 3

Debug: 3.1

Debug: 3.2

Debug: 3.3 - Baro Raspy WIoT Hello world :D

Debug: 3.4

Debug: 3.5

Debug: 3.6

Debug: 3.7

Debug: 3.8

Debug: 4

Il thread 0x64c è terminato con il codice 0 (0x0).

Il thread 0xcfc è terminato con il codice 0 (0x0).

我想我在异步线程上做错了什么,但我就是无法理解。

提前致谢

我自己发现了问题。 http header 格式错误。在从 c# 到 vb 的转换过程中,我没有在 vb 变量中更改此 {bodyStream.Length}

由此

Dim header = "HTTP/1.1 200 OK" & vbCr & vbLf + "Content-Length: {bodyStream.Length}" & vbCr & vbLf + "Connection: close" & vbCr & vbLf & vbCr & vbLf

到此

Dim header = "HTTP/1.1 200 OK" & vbCr & vbLf + "Content-Length: " & bodyStream.Length & vbCr & vbLf + "Connection: close" & vbCr & vbLf & vbCr & vbLf

现在我只希望这段代码对某些人有用,至少我不会浪费那么多时间:)

问题是_Deferral。 捕获 _Deferral。 看这个

Private _Deferral As BackgroundTaskDeferral = Nothing

Public Async Sub Run(taskInstance As IBackgroundTaskInstance) Implements IBackgroundTask.Run
    _Deferral = taskInstance.GetDeferral()
    Dim server As New MyWebserver()
    Await Windows.System.Threading.ThreadPool.RunAsync(AddressOf server.Start)
End Sub

干得好