Why not connect from WinRT app(on StreamSocket) to

2020-05-07 07:49发布

问题:

I'm developing a Windows 8.1 chat client app with metro app and win32 desktop. Problem in not connected from winrt app to win32 app on Tcp socket - exception connected failed with error not reply from other computer. What's the problem?

I'm testing on the locale mashine:

void Connector::ConnectSocketTsp(void)
{
    socket = ref new StreamSocket();

    rootPage->NotifyUser("Connecting to ...", NotifyType::StatusMessage);

    {
        HostName^ serverHost = ref new HostName("127.0.0.1");   //("localhost");
        // Connect...
        create_task(socket->ConnectAsync(
            serverHost, "15500", SocketProtectionLevel::PlainSocket)).then([this](task<void> previousTask)
        {
            try
            {
                previousTask.get();
                rootPage->NotifyUser("Connected", NotifyType::StatusMessage);

                m_reader = ref new DataReader(socket->InputStream);
                m_reader->InputStreamOptions = InputStreamOptions::ReadAhead;
                m_writer = ref new DataWriter(socket->OutputStream);

                ReceiveStringLoop(m_reader, socket);

            }
            catch (Exception^ exception)
            {
                rootPage->NotifyUser("Connect failed with error: " + exception->Message, NotifyType::ErrorMessage);
            }
        });
    }
}

exception->Message = "An attempt to connect was unsuccessful because from another computer in the required time you get the desired response has been disconnected or established connection due to an incorrect response is already connected computer."

And service to translate messages:

void start()
{
    logMessage(QString("Start service"), QtServiceBase::Information);

    QCoreApplication *app = application();

    daemon = new ClientSocket(15500, app);

    if ( !daemon->listen(QHostAddress::LocalHost, 15500) )      // QHostAddress::Any
        logMessage(QString("Failed to bind to port %1").arg(daemon->serverPort()), QtServiceBase::Error);       
    else
        logMessage(QString("Success to bind to port %1").arg(daemon->serverPort()), QtServiceBase::Information);

    if ( !daemon->isListening() )
    {
        logMessage(QString("Failed to bind to port %1").arg(daemon->serverPort()), QtServiceBase::Error);
        app->quit();
    }   
}

Telnet successfully connects to 127.0.0.1 15500...

回答1:

Windows Store apps by default are blocked from connecting to other apps on the same machine.

This block can be suppressed during debugging or (as of the Windows 8.1 Update) bypassed for side-loaded apps (chue x's links and "can't be done" comment are true for Windows 8, but now out of date).

There is no certifiable way to loop back for an app deployed through the store.

See How to enable loopback and troubleshoot network isolation (Windows Store apps) and Using network loopback in side-loaded Windows Store apps