Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot disconnect server child client
Qt 6.11 is out! See what's new in the release blog

Cannot disconnect server child client

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    In my Window Qt application, I've created a server and assigned it to listen to a particular ip address/port. I am able to make connection with a client and communicate with it. However, an issue I seem to be running into is that when my application closes, I am unsuccessfully disconnecting my client socket, causing my server to close unsuccessfully. This then causes my application to unsuccessfully connect to the client the next time my application starts. I will have to reboot the pc to get the port to reset. My current code is as follow:

    @
    // Server code
    Server::Server(QObject *parent)
    : QTcpServer(parent)
    {
    client = NULL;
    connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
    }
    Server::~Server()
    {
    }

    void Server::handleNewConnection()
    {
    client = nextPendingConnection();
    connect(client, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
    }
    void Server::onSocketReadyRead()
    {
    char buffer[1024] = {0};
    client->read(buffer, client->bytesAvailable());
    emit DisplayString(this, QString(&buffer[0]));
    }
    void Server::SendCommand(QString str)
    {
    if (client != NULL)
    {
    int n = client->write(str.toUtf8());
    qDebug() << "length: " << n << "string: " << str;
    }
    }
    void Server::DisconnectClient()
    {
    if (client != NULL && client->isOpen())
    {
    client->disconnectFromHost();
    client->deleteLater();
    client = NULL;
    }
    }

    // instantiating server from main
    server[0] = new Server();
    server[0].listen(QHostAddress::LocalHost, 5000);

    // disconnecting routine in main
    server[0].DisconnectClient();
    server[0].close();

    @

    Can someone tell me how I can disconnect my client socket from my server properly? I am using command line "netstat" to view my port status and the code above does not seem to destroy my client socket. Thanks!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved