0

Why i am not able to connect to a server running on my localhost using telnet client ?
I am using windows-7 & telnet client is turned on in control panel.

Please suggest how to make it working ?

#define SERVER_PORT 5000

Tcp server is created in the tcpserver object :---

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

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

}

// Common slot for the tcpserver - thread

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)
    {
    }

}

Server new connection code :---

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

Now i enter command in telnet :----

C:\Users\Admin> telnet

Welcome to Microsoft Telnet Client

Escape Character is 'CTRL+]'

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

I am able to make my server go in listen mode, as following statement is printed :--

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

But why telnet client is not able to connect to the server running on localhost & PORT = 5000 ?

Katoch
  • 2,709
  • 9
  • 51
  • 84

1 Answers1

1

In the function do work, you have this code: -

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

This is going to stop the current thread from processing messages. If you want to be able to stop the server, have a slot, in the tcpserverobject class, which will close the connection to the QTcpServer when it receives a signal.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • actually this while loop is inside dowork() function of an thread which starts a server .... so you mean to say that ... if i remove this while loop .. then telnet client will be able to connect to the server ..? ... my main problem is telnet client not able to connect to the server ... ? – Katoch Nov 21 '13 at 01:27