Results 1 to 7 of 7

Thread: My QTCPServer crashes

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.