我在VB.net中創建了一個Http服務器。對於每個需要Http服務器的項目,我都會使用相同的「模板」(我相信可以這麼說)(最近不少)。問題是,我正在使用的這一個現在要求局域網上的計算機能夠連接到它。我可以直接從我的電腦訪問服務器,但使用:http://127.0.0.1:8002/
。下面是我的源代碼(脫脂當然下降):爲什麼我的服務器不能在我的電腦外訪問?
Imports System.Net
Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.Win32
Public Class MainWindow
Private Enum ApplicationSituation
Idle
Login
TrackPage
End Enum
Private CurrentApplicationSituation As ApplicationSituation = ApplicationSituation.Idle
Private listener As New HttpListener
Private theService As String
Private Const PORT As Integer = 8002
Private Const SERVERNAME As String = "Poptimas"
Private ContentTypes As New Dictionary(Of String, String)
Private ServerThread As Threading.Thread
Private Sub WriteContentTypes()
'(TEXT) Human-readable text and source code
ContentTypes.Add(".txt", "text/plain")
ContentTypes.Add(".htm", "text/html")
ContentTypes.Add(".html", "text/html")
ContentTypes.Add(".xml", "text/xml")
ContentTypes.Add(".css", "text/css")
ContentTypes.Add(".csv", "text/scv")
ContentTypes.Add(".htc", "text/x-component")
ContentTypes.Add(".js", "application/javascript")
'(IMAGE)
ContentTypes.Add(".png", "image/png")
ContentTypes.Add(".jpg", "image/jpg")
ContentTypes.Add(".bmp", "image/bmp")
ContentTypes.Add(".gif", "image/gif")
ContentTypes.Add(".tiff", "image/tiff")
ContentTypes.Add(".ico", "image/vnd.microsoft.icon")
'(AUDIO)
ContentTypes.Add(".mp3", "audio/mp3")
ContentTypes.Add(".wav", "audio/vnd.wav")
ContentTypes.Add(".ogg", "audio/ogg")
ContentTypes.Add(".mwa", "audio/x-ms-wma")
'(VIDEO)
ContentTypes.Add(".mp4", "video/mp4")
ContentTypes.Add(".wmv", "video/x-ms-wmv")
ContentTypes.Add(".mov", "video/quicktime")
'(APPLICATION) Non-standard
ContentTypes.Add(".pdf", "application/pds")
ContentTypes.Add(".swf", "aplication/x-shockwave-flash")
End Sub
Private Function ParseQuery(ByVal queryset As String) As Dictionary(Of String, String)
Dim FinQu As New Dictionary(Of String, String)
If queryset.Length > 0 Then
For Each qSet As String In queryset.Substring(1).Split("&")
Dim sA() As String = qSet.Split("=")
FinQu.Add(sA(0), sA(1))
Next
End If
Return FinQu
End Function
Private Sub StartServer()
Try
Dim machineName As String = System.Environment.MachineName
VariableValues.Add("port", PORT)
VariableValues.Add("computer", machineName)
theService = HttpUtility.UrlEncode(SERVERNAME)
WriteContentTypes()
With listener
.Prefixes.Add(String.Format("https://*:{0}/", PORT.ToString))
.Prefixes.Add("http://127.0.0.1:" & PORT.ToString & "/")
.Start()
End With
Dim context As HttpListenerContext
Dim path As String
While listener.IsListening
context = listener.GetContext
path = context.Request.Url.AbsolutePath.ToLower
Dim query As Dictionary(Of String, String) = ParseQuery(context.Request.Url.Query)
If path = "/" Then
Select Case CurrentApplicationSituation
Case ApplicationSituation.Idle
Case ApplicationSituation.Login
SendPage(context.Response, My.Computer.FileSystem.CurrentDirectory & "/MobilePandoraConnection/login.html", ".html")
Case ApplicationSituation.TrackPage
SendPage(context.Response, My.Computer.FileSystem.CurrentDirectory & "/MobilePandoraConnection/track.html", ".html")
End Select
ElseIf path = "/inject" Then
SendPage(context.Response, My.Computer.FileSystem.CurrentDirectory & "/MobilePandoraConnection/PandoraMediaServer.js", ".js")
Else
SendPage(context.Response, My.Computer.FileSystem.CurrentDirectory & "/MobilePandoraConnection" & path, path.Substring(path.IndexOf(".")).ToLower())
End If
End While
With listener
.Stop()
.Close()
End With
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub SendPage(ByVal response As HttpListenerResponse, ByVal Location As String, ByVal extension As String)
With response
.ContentType = ContentTypes(extension)
.ContentEncoding = Encoding.UTF8
Try
Dim cont() As Byte = System.Text.Encoding.ASCII.GetBytes(page)
Else
cont = My.Computer.FileSystem.ReadAllBytes(Location)
End If
.OutputStream.Write(cont, 0, cont.LongLength)
.OutputStream.Flush()
Catch ex As Exception
MsgBox("Server Error: " & ex.ToString)
Finally
Try
.Close()
Catch ex As Exception
End Try
End Try
End With
End Sub
Private Sub MainWindow_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Stupid Server likes to keep everything running - kill that sucka'
Process.GetCurrentProcess().Kill()
End Sub
Private Sub SystemPanel_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
'Start server in new thread to prevent an un-reponsive form
ServerThread = New Threading.Thread(AddressOf StartServer)
ServerThread.Start()
End Sub
End Class
如果有什麼不工作(比如,如果你試圖複製和直貼用於測試),我可能只是做而錯誤我正在採取一些非必要的代碼。
而由這路: - 我的防火牆沒有運行
編輯STEVEDOG
我做了這兩個前綴:
.Prefixes.Add(String.Format("https://*:{0}/", PORT.ToString))
.Prefixes.Add(String.Format("http://*:{0}/", PORT.ToString))
現在每次嘗試應用前綴時我都會收到錯誤:
無法偵聽前綴'https:// *:8002 /',因爲它與機器上現有的註冊衝突。
我已經簽了偵聽和8002仍是打開的端口...
你有防火牆嗎? –
@SteveDog好問題。號碼:) – FreeSnow
考慮在將來只發布相關代碼... – 2012-06-05 18:09:41