Results 1 to 7 of 7

Thread: My QTCPServer crashes

  1. #1
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default My QTCPServer crashes

    Hi everybody,

    I'm new to Qt and this forum. After my first steps in the last few weeks with Qt, I tried to write a server which can handle multiple clients. The basis for my program is "Multi client server without threading" of this site.

    My methode Server::start() ist connected with a button, also the methode Server::stop(). When I start my server all works fine, but when I try to stop my server and restart it again with the buttons, my Server crashes as soon as I want to connect with the client. Has anybody of you an idea for a solution for my problem?

    Here my code:

    Qt Code:
    1. // network.h
    2. #ifndef NETWORK_H
    3. #define NETWORK_H
    4.  
    5. #include <QTcpServer>
    6. #include <QTcpSocket>
    7. #include <QHostAddress>
    8. #include <QList>
    9. #include <QHash>
    10. #include <QBuffer>
    11. #include "mainwindow.h"
    12.  
    13. class Server : public QTcpServer {
    14. Q_OBJECT
    15.  
    16. protected:
    17. void sendHello(QTcpSocket *client);
    18.  
    19. protected slots:
    20. void handleNewConnection();
    21. void clientDisconnected();
    22. void start(int port = 10002);
    23. void stop();
    24. void read();
    25.  
    26. private:
    27. QList<QTcpSocket *> clientConnections;
    28. QHash<QTcpSocket*, QBuffer*> buffers;
    29.  
    30. public:
    31. explicit Server();
    32. ~Server();
    33. };
    34.  
    35.  
    36. #endif // NETWORK_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // network.c
    2. #include "network.h"
    3.  
    4. Server::Server() {
    5. }
    6.  
    7. Server::~Server() {
    8. stop();
    9. }
    10.  
    11. void Server::stop() {
    12. close();
    13. qDeleteAll(clientConnections);
    14. }
    15.  
    16. void Server::start(int port) {
    17. if(!listen(QHostAddress::Any, port)) {
    18. return;
    19. }
    20. connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
    21. }
    22.  
    23. void Server::handleNewConnection() {
    24. while (hasPendingConnections()) {
    25. QTcpSocket *client = nextPendingConnection();
    26. QBuffer* buffer = new QBuffer(this);
    27. buffer->open(QIODevice::ReadWrite);
    28. buffers.insert(client, buffer);
    29. connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    30. connect(client, SIGNAL(readyRead()), this, SLOT(read()));
    31. clientConnections.append(client);
    32.  
    33. sendHello(client);
    34. }
    35. }
    36.  
    37. void Server::clientDisconnected() {
    38. QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
    39.  
    40. if(!client) return;
    41.  
    42. clientConnections.removeAll(client);
    43. client->deleteLater();
    44. }
    45.  
    46. void Server::sendHello(QTcpSocket *client) {
    47. if(!client) return;
    48. client->write("Hello\n");
    49. }
    50.  
    51. void Server::read() {
    52. QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
    53. QBuffer* buffer = buffers.value(client);
    54.  
    55. if(!client) return;
    56.  
    57. qint64 bytes = buffer->write(client->readAll());
    58. buffer->seek(buffer->pos() - bytes);
    59. while (buffer->canReadLine()) {
    60.  
    61. QByteArray line = buffer->readLine();
    62.  
    63. switch(line[0]) {
    64. case 'a' : client->write(line);
    65. break;
    66. }
    67. }
    68. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance.
    Last edited by Mr_Burns; 23rd May 2011 at 17:58.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: My QTCPServer crashes

    Did you try debugging? where does it crash? have a look at the stack when it crashes, it should give you very good idea what went worng.

  3. #3
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My QTCPServer crashes

    Hi Santosh Reddy,

    thanks for your fast answer ;-). No, I haven't debugged my code, yet, but I did it after your post. I've only checked the issues from my compiler, before. But now, with debugging, it shows like qDeleteAll is the problem. Exactly, on this PC my program is crashing when I close my server in second time. I did the following steps:

    1. run Server::start()
    2. connected with an terminal
    3. run Server::stop()
    4. run Server::start()
    5. connected again with an terminal
    6. run Server::stop() --> my program crashed

    Comment: at another computer some hours ago, my program crashed at point 5, as I wrote in my first post.

    Do you have an idea what I did wrong? How I can look at my stack?

    Best regards
    Mr_Burns

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: My QTCPServer crashes

    If you use QtCreator, then this is the screenshot, else refer the IDE docs.
    Debug-Stack.jpg


    Added after 6 minutes:


    You should be using either
    Qt Code:
    1. qDeleteAll()
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. deleteLater()
    To copy to clipboard, switch view to plain text mode 
    not both.

    You may want to replace
    Qt Code:
    1. qDeleteAll(XXXX); //with XXXX.clear();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 23rd May 2011 at 19:32.

  5. #5
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My QTCPServer crashes

    Quote Originally Posted by Santosh Reddy View Post
    You may want to replace
    Qt Code:
    1. qDeleteAll(XXXX); //with XXXX.clear();
    To copy to clipboard, switch view to plain text mode 
    When I do it, my program doesn't crash now ;-). My server did not accept new connetions but my active connections to my clients were not killed. But why? - Normally I think xxx.clear() removed all items from my list and with them my connections must be closed or did I understand anything wrong?

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: My QTCPServer crashes

    xxx.clear() will only remove the Objects from the list (in this case they happen to be pointers to the clients), they aer just removed from the list not really deleted / closed.

    If I understand you code correctly you wanted to delete the clients in
    Qt Code:
    1. void Server::clientDisconnected() {
    2. QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
    3.  
    4. if(!client) return;
    5.  
    6. clientConnections.removeAll(client);
    7. client->deleteLater();
    8. }
    9.  
    10. ...
    11. connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); //This is supposed to make the connection
    12. ...
    To copy to clipboard, switch view to plain text mode 

    try adding following to your code (or somthing like this, which will trigger the above connection)

    Qt Code:
    1. void Server::stop() {
    2. QTcpSocket *client;
    3. while(clientConnections.count() > 0)
    4. {
    5. client = clientConnections.takeFirst(); // this will both remove and return the object, so xxxx.clear() is not needed
    6. client.disconnectFromHost ();
    7. }
    8. close();
    9. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Santosh Reddy for this useful post:

    Mr_Burns (23rd May 2011)

  8. #7
    Join Date
    May 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My QTCPServer crashes

    Hi Santosh Reddy,

    okay, now I understand what xxx.clear() and your code do. Finally my little program works fine and learned a lot ;-).
    Thank you for your help !!!!

    Best regards,
    Mr_Burns

Similar Threads

  1. Problems with QTcpServer
    By elcuco in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2011, 21:17
  2. My server (using QTcpServer and QTcpSocket) crashes
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2010, 15:32
  3. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  4. QThread + QTcpServer
    By Fastman in forum Qt Programming
    Replies: 4
    Last Post: 19th July 2007, 16:19
  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
  •  
Qt is a trademark of The Qt Company.