Results 1 to 3 of 3

Thread: [QTcpServer] Lost Connection

  1. #1
    Join Date
    Aug 2011
    Location
    Germany
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [QTcpServer] Lost Connection

    Hi,

    I'm struggeling about how to disconnect a TCP connection correctly when this connection fails, e.g. when unplugging the ethernet cable.
    I have a QTcpServer which is listening for any client at one specific port. When a client gets connected, my slot connectNewClient is triggered. When I close my connection I can reconnect without any problem.
    But when my connection is closed by any unexpected purpose (e.g. unplugging the cable), my slot myClientDisconnected is triggered. But when I'm trying to reconnect, the client isn't able to connect again. I have to do a reboot of my (Windows-) computer. Just disabling and enabling my NIC doesn't help.
    All TCP-Client-Server examples instantaneously disconnect the client when data was sent. But my client-server-connection should keep opened when connected once.
    I hope s.o. can help me.

    I have this code:
    my header:
    Qt Code:
    1. bool m_bConnected;
    2. QTcpServer *m_server;
    3. QTcpSocket *client;
    To copy to clipboard, switch view to plain text mode 
    Init:
    Qt Code:
    1. m_server = new QTcpServer(this);
    2. connect(m_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
    3. m_server->listen(QHostAddress::Any, MY_PORT);
    To copy to clipboard, switch view to plain text mode 

    Connect:
    Qt Code:
    1. void ethComm::connectNewClient(void)
    2. {
    3. client = m_server->nextPendingConnection();
    4. connect(this, SIGNAL(DisconnectClient()), client, SLOT(deleteLater()));
    5. connect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
    6. m_bConnected = true;
    7. connect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    when disconnected:
    Qt Code:
    1. void ethComm::myClientDisconnected(void)
    2. {
    3. m_bConnected = false;
    4.  
    5. disconnect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
    6. disconnect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));
    7.  
    8. client->close();
    9.  
    10. emit DisconnectClient();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Destructor:
    Qt Code:
    1. ethComm::~ethComm(void)
    2. {
    3. if(m_bConnected)
    4. client->close();
    5. if(m_server)
    6. delete(m_server);
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2011
    Location
    Germany
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QTcpServer] Lost Connection

    some more additional information:

    my server tries to send data to the client, but as the TCP packet isn't ACK-ed by the client, KeepAlive-messages are send. If these 5 KeepAlive-messages fail, my QTcpServer recognized that the client is disconencted (as the signal "disconnected" is emitted).

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QTcpServer] Lost Connection

    So what is the problem exactly? Technically speaking unpluging the network cable from a computer DOES NOT break a TCP connection.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 19:18
  2. Replies: 0
    Last Post: 1st August 2011, 13:06
  3. Qt and OpenGl (I'm lost)
    By anon789 in forum Newbie
    Replies: 6
    Last Post: 17th September 2010, 20:09
  4. Replies: 1
    Last Post: 2nd April 2010, 06:42
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.