QTcpSocket emits "Connection Refused Error" after few Successful connection?

skg picture skg · Nov 2, 2012 · Viewed 8.1k times · Source

I am designing an application for accessing a remote desktop using Qt creator. In order to get "Quit" Signal from the Remote desktop (after completing my purpose), i am using Tcpserver and Tcpsocket. My Pc acts as a Server while remote pc acts as a Client. I am using a following concept:

Server pc 1. Press PushButton to access the remote screen (in full screen mode using tightvnc). 2. Start server and listen for any active connection (i am using port 9876). 3. Active connection found. Connected to the client. 4. Close the remote access. 5. switch back to the local screen. 6. Server Close

Client Pc 1. Press Quit button to close Remote access. 2. When Quit button pressed 3. connect to host. 4. send "Quit" to the server 5. disconnect from host 6. close connection.

It works fine for few attempts ( lets say 10 times)

but there after it starts giving error "Connection refused Error". And I am unable to get back from the remote access untill i reboot my remote Pc.

I have tried using Reset but the result is same.

Any one have any idea ???

Here is my code for Client side

#include "ctrlboardclient.h"
#include <QHostAddress>
#include <QObject>
#include <QtGui/QApplication>
#include <QDebug>


bool CtrlBoardClient::status_flag = false;           /* Flag to check the transfer status of Data */


CtrlBoardClient::CtrlBoardClient()
{
    connect(&client, SIGNAL(connected()),    this,  SLOT(startTransfer()));
    connect(&client, SIGNAL(readyRead()),    this,  SLOT(recieve_msg()));
    connect(&client, SIGNAL(disconnected()), this,  SLOT(disconnectstatus()));
    connect(&client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(getErrorCode(QAbstractSocket::SocketError)));
}


bool CtrlBoardClient::start(QString address, quint16 port)
{
    QHostAddress addr(address);
    bool rval = client.reset();
    qDebug() << "Reset before Connect to Host = " << rval;
    client.connectToHost(address, port);

    if (!client.waitForConnected(3000)) {
        bool rval = client.reset();
        qDebug() << "Reset after Connect to Host = " << rval;
        qDebug() << "Client is unable to connect to Server ";
        return false;
    }
    qDebug() << "Client Server Connection Successful";
    status_flag = false;
    return true;
}

void CtrlBoardClient::getErrorCode(QAbstractSocket::SocketError errorCode)
{
    qDebug() << "Socket Error = " << errorCode;
}


void CtrlBoardClient::SendMessage(QString Message)
{
    status_flag = true;
    msg = Message;
    startTransfer();
    qDebug() << "Message sent to the Server";
    client.disconnectFromHost();
    while (!client.waitForDisconnected(3000));
    qDebug() << "Disconnected from the Host";
    return;
}


void CtrlBoardClient::startTransfer()
{
    if (status_flag) {
        QByteArray block = "";
        block.append(msg);
        client.write(block);
    }

    status_flag = false;
    return;
}


QByteArray CtrlBoardClient::RecieveMessage()
{
    return indata;
}


void CtrlBoardClient::recieve_msg()
{
    indata = "";
    indata.append(client.readAll());
    emit recievemsg();
}


void CtrlBoardClient::disconnectstatus()
{
    qDebug() << "Closing Client connection";
    CloseClientConnection();
    emit connection_aborted();
}


void CtrlBoardClient::CloseClientConnection()
{
    bool rval = client.reset();
    qDebug() << "Reset after Disconnect from Host = " << rval;
    client.close();
}

My Server code is:

#include "mainboardserver.h"

MainBoardServer::MainBoardServer()
{
   connect(&mainserver, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
   connect(this, SIGNAL(disconnected()), this, SLOT(DisconnectMessage()) );

    if (!mainserver.listen(QHostAddress::Any, 9876)) {
        emit no_incoming_connection();
    }
}

void MainBoardServer::acceptConnection()
{
    ctrlclient = mainserver.nextPendingConnection();
    connect(ctrlclient, SIGNAL(readyRead()), this, SLOT(startRead()));
    connect(ctrlclient, SIGNAL(disconnected()), this, SLOT(DisconnectMessage()));
    emit connection_found();
}

void MainBoardServer::startRead()
{
    char buffer[1024] = {0};
    ClientChat = "";
    ctrlclient->read(buffer, ctrlclient->bytesAvailable());
    ClientChat.append(buffer);
    ctrlclient->close();
    emit data_recieved();
}

QString MainBoardServer::RecieveData()
{
    return ClientChat;
}

void MainBoardServer::TransferData(QByteArray data)
{
    ctrlclient->write(data);
}

void MainBoardServer::DisconnectMessage()
{
    emit connection_lost();
}


void MainBoardServer::closeServer()
{
    mainserver.close();
    emit disconnected();
}

Any idea how to resolve this issue ?? what mistake i am committing ???

Answer

Remy Lebeau picture Remy Lebeau · Nov 2, 2012

I don't know anything about Qt, but a "Connection Refused" socket error means one of two possibilities:

1) there is no server socket listening at the target IP/Port at all.

2) there is a server socket listening, but its backlog of pending client connections is full and cannot accept a new connection at that moment. Either the server code has stopped calling accept() altogether, or is not calling it fast enough to satisfy the number of clients that are trying to connect.

Either way, there is no way for a client to know on its end which condition is actually occurring. All it can do is wait a little while and try connecting again.

You need to diagnose your server code, not your client code. Your server code is not behaving the way you are expecting.