telnet客户端没有连接 - 到QTCPserver

时间:2013-11-20 11:24:38

标签: qt telnet qtcpserver

为什么我无法使用telnet客户端连接到我的localhost上运行的服务器? 我正在使用Windows-7& telnet客户端在控制面板中打开。

请建议如何使其正常工作?

#define SERVER_PORT 5000

在tcpserver对象中创建Tcp服务器:---

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&amp; PORT = 5000 ?

1 个答案:

答案 0 :(得分:1)

在函数do work中,您有以下代码: -

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

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