2012-06-05 65 views
1

我在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仍是打開的端口...

+1

你有防火牆嗎? –

+0

@SteveDog好問題。號碼:) – FreeSnow

+1

考慮在將來只發布相關代碼... – 2012-06-05 18:09:41

回答

3

不能監聽IP地址127.0.0.1。您需要真正收聽您的真實IP地址。在127.0.0.1上收聽只會收到專門發送到回送地址的消息。

此外,您不能使用HttpListener在同一端口和IP地址上同時監聽http和https。請參閱此鏈接的詳細信息:

.NET HttpListener: when registering both HTTP & HTTPS I get "conflicts with an existing registration on the machine"

+0

我很直接,它仍然不起作用。同時,我還使用本地IP地址在端口8001上運行商用服務器,並且工作正常。我嘗試使用'http://192.168.1.1:8002'連接到我的電腦服務器。商業服務器工作正常,我的不是。 – FreeSnow

+0

因此,您將'.Prefixes.Add(「http://127.0.0.1:」&PORT.ToString&「/」)'行更改爲'.Prefixes.Add(「http://192.168.1.1:」& PORT.ToString&「/」)'它仍然不起作用? –

+0

@XanderLamkins爲什麼你在https前綴上有星號,但不在http前綴上?您是否嘗試將http前綴更改爲使用星號而不是特定的IP地址?你嘗試過使用+而不是*嗎? –

0

http://127.0.0.1:8002/,您的計算機的地址爲127.0.0.1

通常有4種方式來訪問你的服務器:

  • 127.0.0.1:這隻會從自己的計算機工作。

  • 192.168.1.1或類似:這隻適用於您的本地網絡。由於IPV4地址的數量非常有限,因此您使用的地址只能在您的局域網上進行升級,並且沒有每個人都可以看到的「永久」IP地址。局域網中的每臺計算機都有不同的本地IP地址,但世界上有許多計算機具有相同的本地地址。

  • 如果您的局域網和互聯網之間的路由器配置爲允許輸入來自互聯網的連接(這對您來說有點危險,特別是在您的個人計算機上沒有防火牆的情況下),那麼您有一個IP,您可以知道使用很多在線服務,如http://www.knowmyip.com/。這個地址是臨時的,並且會在你的下一個連接時改變(因爲IPV4地址不夠)這樣人們可以使用類似http://keods.dyndns.org的東西訪問您的服務器,而不是詢問您當前的IP地址是什麼。

但只要你的服務器是沒有一個固定的IP地址在專用網絡上,您的服務器可能不會那麼容易不是像http://www.ovh.co.uk/由專門的公司專門託管的服務器(或一個又一個訪問,這只是一個例子)。在向商業合作伙伴提供地址之前,您應該選擇衆多命名和託管服務中的一種,其中大部分都非常便宜。

相關問題