gpt4 book ai didi

如果远程 ip C# httplistener 错误

转载 作者:行者123 更新时间:2023-11-30 16:25:33 28 4
gpt4 key购买 nike

我创建了一个 httplistener,它只能使用 localhost 作为前缀。如果我将它更改为远程服务器 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();

这是网络客户端:

   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");

只有当我将“我的公共(public) ip”更改为我的本地 ip 时它才有效有什么想法吗?

最佳答案

您的 Windows 防火墙是否开启?尝试禁用它,看看是否有帮助。您可能需要实际禁用 Windows 防火墙服务(在您的本地服务列表中)。

关于如果远程 ip C# httplistener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9763165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com