无法连接到 Windows 上的本地服务器

Can't connect to local server on Windows

本文关键字:服务器 Windows 连接      更新时间:2023-10-16

我的服务器应用程序使用POCO库用C++编写时遇到了一个奇怪的问题。它运行良好,几天前刚刚停止工作。即使使用两周前的exe也不起作用。

问题是,我无法通过localhost连接到服务器,但来自我网络之外的其他人可以毫无问题地连接。。。

我在其他电脑上测试过,除了一台电脑外,其他电脑都是一样的。该问题仅在Windows上存在。我在Linux上进行了测试,它运行良好。

该问题也在POCO教程的代码中重现http://pocoproject.org/slides/200-Network.pdf:

#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/SocketAddress.h"
int main(int argc, char** argv)
{
Poco::Net::ServerSocket srv(8080); // does bind + listen
for (;;)
{
Poco::Net::StreamSocket ss = srv.acceptConnection();
Poco::Net::SocketStream str(ss);
str << "HTTP/1.0 200 OKrn"
"Content-Type: text/htmlrn"
"rn"
"<html><head><title>My 1st Web Server</title></head>"
"<body><h1>Hello, world!</h1></body></html>"
<< std::flush;
}
return 0;
}

在Chrome中输入127.0.0.1:8080会导致ERR_CONNECTION_ABORTED。http://www.canyouseeme.org/报告它可以在8080端口上看到我的服务。

其他应用程序(不使用POCO I guess?)运行良好。

我完全不知道这个问题的原因是什么。。。如果有任何建议,我将不胜感激。

我试过你的例子。这看起来很正常。如果它过去是有效的,那么它就不正常了。可能是,你已经更新了Chrome浏览器,或者之前用一些旧浏览器测试过吗?

在该示例中,服务器正在发送数据并立即关闭连接。我认为,POCO中应该有一个命令,以任何更优雅的方式终止连接。

该示例似乎演示了非常简单但不符合协议的服务器响应。请尝试使用HTTPRequestHandlerFactory。这样,服务器似乎不会在每次请求后终止连接。

Firefox显示非常短的"Hello World!">,然后显示"连接中止">

Chrome中的ERR_CONNECTION_ABORTED可能也意味着服务器意外关闭了连接。

卷曲显示在大多数情况下:

C:bin>curl.exe http://localhost:8080
curl: (56) Recv failure: Connection was reset

但在一些罕见的情况下:

C:bin>curl.exe http://localhost:8080
<html><head><title>My 1st Web Server</title></head><body><h1>Hello, world!</h1></body></html>
curl: (56) Recv failure: Connection was reset

flush之后添加了Sleep(100)之后,html内容总是显示在Recv failure之前。因此,取决于浏览器是否显示内容的时间。

可能有关于linux上不同行为的提示:

void HTTPServerConnection::onServerStopped(const bool& abortCurrent)
{
_stopped = true;
if (abortCurrent)
{
try
{
// Note: On Windows, select() will not return if one of its socket is being
// shut down. Therefore we have to call close(), which works better.
// On other platforms, we do the more graceful thing.
#if defined(_WIN32)
socket().close();
#else
socket().shutdown();