Results 1 to 2 of 2

Thread: qtcpsocket server issues

  1. #1
    Join Date
    Aug 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default qtcpsocket server issues

    Hi I am somewhat new to network programming and I've read and have redone my server a few times trying to get it right, i had a hard time getting ssl self signed to work but i have a grasp on it now.

    My server is going to have agents connect to it every so often and pass some data between the server and agent and then disconnect. I have it working for the most part but what happens is the server will disconnect the socket even though i never said to, i think this is because its a qRunnable and doesnt do slots..... Also in my thread i have my agents right now pass a google protocol buffer, its basically a hello world until i get this server working. The qdebug output shows up in the console after the client gets disconnected...I dont understand why that is. This might just be my lack of knowledge on how qtcpsockets work so i need some expertise in this. Is there a better way I should be doing this?

    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "myserver.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. MyServer server;
    9. server.StartServer();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    myserver.cpp
    Qt Code:
    1. #include "myserver.h"
    2.  
    3. MyServer::MyServer(QObject *parent):
    4. QTcpServer(parent)
    5. {
    6. //Create a threadpool
    7. pool = new QThreadPool(this);
    8.  
    9. //How many threads I want at any given time
    10. //If there are more connections, they will be qued until a threads is closed
    11. pool->setMaxThreadCount(5); // ex. 5 threads
    12. }
    13.  
    14.  
    15. void MyServer::StartServer()
    16. {
    17. if (!this->listen(QHostAddress::Any, 5656))
    18. {
    19. qDebug() << "Unable to start the server: " << this->errorString();
    20. }
    21. else
    22. {
    23. //Server is now listening..
    24. qDebug() << "autm8 started and is listning on port 5656" << QTcpServer::serverAddress();
    25.  
    26. }
    27. }
    28.  
    29.  
    30. void MyServer::incomingConnection(int handle) //Handle incoming connections
    31. {
    32. MyRunnable *task = new MyRunnable();
    33. task->setAutoDelete(true); //Delete that object when you're done (instead of using signals and slots)
    34.  
    35. task->socketDescriptor = handle;
    36.  
    37. pool->start(task); //Uses a QRunnable object
    38. }
    To copy to clipboard, switch view to plain text mode 

    myrunnable.cpp
    Qt Code:
    1. #include "myrunnable.h"
    2. #include <QSslSocket>
    3. #include "pb/client.pb.h"
    4. #include <QDebug>
    5. MyRunnable::MyRunnable()
    6. {
    7.  
    8. }
    9.  
    10. void MyRunnable::run()
    11. {
    12. if (!socketDescriptor)
    13. return;
    14.  
    15. QSslSocket socket;
    16.  
    17. socket.setSocketDescriptor(socketDescriptor);
    18. QString key = "server.key";
    19. socket.setPrivateKey(key);
    20. QString cert = "server.csr";
    21. socket.setLocalCertificate(cert);
    22. socket.setPeerVerifyMode(QSslSocket::VerifyNone);
    23. socket.startServerEncryption();
    24. socket.waitForEncrypted(3000);
    25.  
    26.  
    27. if(socket.isEncrypted())
    28. {
    29. qDebug() << "Encrypted";
    30. socket.waitForReadyRead(3000);
    31. QByteArray data = socket.readAll();
    32. socket.waitForReadyRead();
    33. //Google ProtoBuff Test
    34. Client client;
    35. client.ParseFromArray(data, data.size());
    36. std::string hostname = client.hostname();
    37. QString out = QString::fromStdString(hostname);
    38. qDebug() << out;
    39.  
    40. }
    41. else
    42. {
    43. qDebug() << "Not Encrypted";
    44. }
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 

    If headers are needed let me know, i appreciate any feedback. Thanks.

  2. #2
    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: qtcpsocket server issues

    Your runnable ends and all its data (including the socket) is removed and thus the connection is broken. I'm repeating this two times a month on average but I'll do it again -- you don't need threads for networking. Use signals and slots and handle everything in a single thread.
    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. saving data to server through QTcpSocket
    By toufic.dbouk in forum Qt Programming
    Replies: 3
    Last Post: 6th January 2013, 22:48
  2. QTcpSocket/QTcpServer on a heavy load server
    By Eumcoz in forum Qt Programming
    Replies: 10
    Last Post: 7th August 2012, 20:36
  3. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 09:45
  4. Replies: 0
    Last Post: 5th February 2011, 09:31
  5. My server (using QTcpServer and QTcpSocket) crashes
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2010, 16:32

Tags for this Thread

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.