2013-11-20 73 views
0

爲什麼我無法使用telnet客戶端連接到在本地主機上運行的服務器?
我正在使用windows-7 & telnet客戶端在控制面板中打開。遠程登錄客戶端未連接 - 到QTCPserver

請建議如何讓它工作?

#define SERVER_PORT 5000 

TCP服務器在tcpserver的對象創建:---爲tcpserver的

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

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

} 

//通用插槽 - 螺紋

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 ..!!!"; 
} 

現在我輸入c ommand在遠程登錄:----

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客戶端不能連接到本地主機上&端口上運行的服務器= 5000 ?

回答

1

在功能方面做的工作,你有這樣的代碼: -

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

這將停止處理消息的當前線程。如果您希望能夠停止服務器,請在tcpserverobject類中有一個插槽,它將在接收信號時關閉與QTcpServer的連接。

+0

實際上,這個while循環在啓動服務器的線程的dowork()函數中....所以你的意思是說...如果我刪除這個while循環..然後telnet客戶端將能夠連接到服務器 ..?我的主要問題是telnet客戶端無法連接到服務器...? – Katoch

+0

請在這一點上建議......? – Katoch

+1

其工作...剛剛刪除,而while循環.. – Katoch

相關問題