0

为什么我无法使用 telnet 客户端连接到在我的本地主机上运行的服务器?
我正在使用 windows-7 并且在控制面板中打开了 telnet 客户端。

请建议如何使它工作?

#define SERVER_PORT 5000

tcp server 是在 tcpserver 对象中创建的:---

tcpserverobject::tcpserverobject(QObject *parent) :
    QObject(parent), tcpServer(0)
{
    tcpServer = new QTcpServer;

    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(on_newConnection()));

}

// tcpserver 的公共槽 - 线程

void tcpserverobject::dowork()
{
    if (!tcpServer->listen(QHostAddress::LocalHost, SERVER_PORT )) {

        qDebug() << "\n returning from server listning error .. !!! ";

        return;
    }

    qDebug() << "\n server listning";


    //while(1)
    while(!m_bQuit)
    {
    }

}

服务器新连接代码:---

void tcpserverobject::on_newConnection()
{
    QByteArray block;

    block.append(" \n Hello from server .. !!!") ;

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
                clientConnection, SLOT(deleteLater()));

    // Create new thread for this .. client request ..!!
    qDebug() << "\n New connection request ..!!!";
    qDebug() << "\n New client from:" << clientConnection->peerAddress().toString();

    clientConnection->write(block);
    clientConnection->flush();

    clientConnection->disconnectFromHost();
    qDebug() << "\n New connection request closed ..!!!";
}

现在我在 telnet 中输入命令:----

C:\Users\Admin> telnet

Welcome to Microsoft Telnet Client

Escape Character is 'CTRL+]'

Microsoft Telnet> open localhost 5000
Connecting To localhost...

我可以让我的服务器进入侦听模式,因为打印了以下语句:-

qDebug() << "\n server listning";

但是为什么 telnet 客户端无法连接到在 localhost & PORT = 5000 上运行的服务器?

4

1 回答 1

1

在功能做的工作,你有这个代码: -

//while(1)
while(!m_bQuit)
{
}

这将阻止当前线程处理消息。如果您希望能够停止服务器,请在 tcpserverobject 类中设置一个插槽,该插槽将在收到信号时关闭与 QTcpServer 的连接。

于 2013-11-20T11:31:07.023 回答