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.