PDA

View Full Version : qtcpsocket server issues



scottscreations
11th August 2013, 20:26
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


#include <QCoreApplication>
#include "myserver.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

MyServer server;
server.StartServer();

return a.exec();
}


myserver.cpp


#include "myserver.h"

MyServer::MyServer(QObject *parent):
QTcpServer(parent)
{
//Create a threadpool
pool = new QThreadPool(this);

//How many threads I want at any given time
//If there are more connections, they will be qued until a threads is closed
pool->setMaxThreadCount(5); // ex. 5 threads
}


void MyServer::StartServer()
{
if (!this->listen(QHostAddress::Any, 5656))
{
qDebug() << "Unable to start the server: " << this->errorString();
}
else
{
//Server is now listening..
qDebug() << "autm8 started and is listning on port 5656" << QTcpServer::serverAddress();

}
}


void MyServer::incomingConnection(int handle) //Handle incoming connections
{
MyRunnable *task = new MyRunnable();
task->setAutoDelete(true); //Delete that object when you're done (instead of using signals and slots)

task->socketDescriptor = handle;

pool->start(task); //Uses a QRunnable object
}


myrunnable.cpp


#include "myrunnable.h"
#include <QSslSocket>
#include "pb/client.pb.h"
#include <QDebug>
MyRunnable::MyRunnable()
{

}

void MyRunnable::run()
{
if (!socketDescriptor)
return;

QSslSocket socket;

socket.setSocketDescriptor(socketDescriptor);
QString key = "server.key";
socket.setPrivateKey(key);
QString cert = "server.csr";
socket.setLocalCertificate(cert);
socket.setPeerVerifyMode(QSslSocket::VerifyNone);
socket.startServerEncryption();
socket.waitForEncrypted(3000);


if(socket.isEncrypted())
{
qDebug() << "Encrypted";
socket.waitForReadyRead(3000);
QByteArray data = socket.readAll();
socket.waitForReadyRead();
//Google ProtoBuff Test
Client client;
client.ParseFromArray(data, data.size());
std::string hostname = client.hostname();
QString out = QString::fromStdString(hostname);
qDebug() << out;

}
else
{
qDebug() << "Not Encrypted";
}

}


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

wysota
11th August 2013, 20:35
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.