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

时间:2015-04-20 20:56:56

标签: c++ windows poco-libraries

我使用POCO库用C ++编写的服务器应用程序有一个奇怪的问题。它工作正常,几天前停止工作。即使使用2周前的exe也不行。

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

我已经在其他计算机上测试了它,除了一台计算机外,它是相同的。 该问题仅存在于Windows上。我已经在Linux上测试过了,它运行良好。

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

#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 OK\r\n"
        "Content-Type: text/html\r\n"
        "\r\n"
        "<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?)工作正常。

我完全不知道这个问题的原因是什么...... 我会很感激任何建议。

1 个答案:

答案 0 :(得分:0)

我试过你的榜样。看起来像是正常行为。如果它曾经工作过,那么它并不正常。可能是,您之前使用过某些旧浏览器更新了Chrome或进行了测试?

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

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

Firefox显示非常短的&#34; Hello World!&#34; 然后&#34;连接已中止&#34;

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

在我Sleep(100)之后添加了flush之后,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();