2012-03-18 24 views
3

我創建了一個httplistener,它只適用於localhost作爲前綴。如果將其更改爲遠程服務器IP,則會顯示錯誤。C#httplistener錯誤,如果遠程IP

下面的代碼:

HttpListener listener = new HttpListener(); 
     listener.Prefixes.Add("http://*:8080/"); // I need localhost replaced with remote ip 
     listener.Start(); 
     while (true) 
     { 
      // Wait for a client request: 
      HttpListenerContext context = listener.GetContext(); 

      // Respond to the request: 
      string msg = "You asked for: " + context.Request.RawUrl; 
      context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg); 
      context.Response.StatusCode = (int)HttpStatusCode.OK; 

      using (Stream s = context.Response.OutputStream) 
      using (StreamWriter writer = new StreamWriter(s)) 
       writer.Write(msg); 
     } 
     listener.Stop(); 

這是Web客戶端:

using (WebClient wc = new WebClient())  // Make a client request. 
        wc.DownloadString 
         ("http://"my public ip"(on my router):8080/lg.txt"); //port forwarding is set 
       MessageBox.Show("received"); 

如果我改變 「我的公網IP」 我的本地IP 任何想法,它僅?

+2

「遠程服務器IP」 - 遠程誰?它必須是本地的服務器... – 2012-03-18 23:14:29

+0

@Marc我的意思是我的公共IP地址 – Igor 2012-03-18 23:16:20

回答

2

是否打開了Windows防火牆?嘗試禁用它,看看是否有幫助。您可能需要實際禁用Windows防火牆服務(在本地服務列表中)。

+0

是的,就是這樣!任何方式來編程添加防火牆免除規則??? – Igor 2012-03-18 23:30:53

+0

不是我所知...我認爲這將是一個相當大的安全漏洞(如果一個程序可以添加防火牆規則 - 一個惡意程序可以打開你的計算機到世界)。 – MattW 2012-03-19 12:24:32

1

首先,檢查防火牆。不過,也許更可能的是http.sys,它由HttpListener使用 - 您必須通過「netsh」將該前綴提供給該帳戶。您可以通過簡單地以提升的管理員帳戶運行來檢查此問題 - 如果它起作用,則可能是http.sys權限問題。

+1

這是防火牆。 – Igor 2012-03-18 23:34:45