我創建了一個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 任何想法,它僅?
「遠程服務器IP」 - 遠程誰?它必須是本地的服務器... – 2012-03-18 23:14:29
@Marc我的意思是我的公共IP地址 – Igor 2012-03-18 23:16:20