|
我查看了HttpListener类的MSDN文档,测试它提供的例子 class Program
{
static void Main(string[] args)
{
SimpleListenerExample(new string[]{"http://*:8080/"});
}
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported) {
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes) {
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
}
在代码中将URL设置为”http://*:8080/”,启动运行,并在浏览器输入http://127.0.0.1:8080/或http:localhost:8080/都能监听返回信息Hello world! 我将URL设置”http://192.168.1.126:8080/后运行,也是输入127.0.0.1和localhost能监听,而输入固定的IP地址时不能监听 这是怎么回事???求帮忙! |
|
| 20分 |
看下防火墙,8080端口加入白名单了吗
你用127.0.0.1或localhost的时候,是直接内存映射的,不走网卡,当然也不走防火墙 你把网线拔掉,127.0.0.1也是能ping通的 此外,看看网线插了没有,网线没插,当然IP也是没法访问的 |
|
有一些端口是不需要加入白名单,默认就允许出入的
比如80端口 而8080是你自定义端口,不加入白名单,默认是禁止的 |
|
|
如果是防火墙问题,我启动Tomcat服务,也是用8080端口,局域网机子应该不能访问才对,但测试结果是可以访问。 Win7系统没有防火墙白名单呀,怎么办? |
|
| 10分 |
先确定是不是防火墙的问题,把防火墙关了试试
|
|
关闭防火墙测试,还是不行,应该不防火墙问题吧,现在更没思路了 |
|
|
关闭防火墙后,也换过其他端口测试了,还是不行
|
|
|
不好意思,刚才关闭防火墙测试,是本地的浏览器输入固定IP访问不了的,现在用局域网其他电脑能正常访问了,现在知道是防火墙问题了。 但我不能关闭防火墙运行程序的,有什么好的方法解决? |
|
|
我在另一个帖子的一段代码:http://bbs.csdn.net/topics/390963758
看看哪里的个别字符不一样。 |
|
| 10分 |
要让其它电脑访问,你需要把 端口号 加入防火墙的“例外”(或者“出栈、入栈”)规则中。 |
太好了,太感谢了,在高级windows防火墙添加了端口的入站和出站规则(Win7),测试通过了。 不知道有没有DOS命令可以修改防火墙规则? |
|
|
防火墙操作DOS命令
http://blog.csdn.net/hyholine/article/details/7729172 在服务监听启动之前添加打开端口的方法: private static void AddFireWallPort(int port)
{
string argsDll = String.Format(@"firewall set portopening TCP {0} ENABLE", port);
ProcessStartInfo psi = new ProcessStartInfo("netsh", argsDll);
psi.Verb = "runas";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process.Start(psi).WaitForExit();
}
测试通过,可以结贴了,感谢各位大牛的帮助! |
|