Reputation: 2777
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 ?
Upvotes: 0
Views: 850
Reputation: 27611
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.
Upvotes: 1