QCoreApplication QTcpSocket readyRead() signal ?
-
The problem is that:
I have QTcpSocket Application and in GUI mode it works, but when I set console parameters QTcpSocket does not emit readyRead() signal !!!!
I'm working in Ubuntu 12.04QTcpServer:
/* * New client connection */ void SenseSystem::TechServer::newclient() { // If server started qDebug() << QString::fromUtf8("New connection!!!"); qintptr IDSocket = tcpServer->nextPendingConnection()->socketDescriptor(); QThread *thread = new QThread(); Client *client = new Client(IDSocket); client->moveToThread(thread); connect(thread,SIGNAL(started()),client,SLOT(process())); connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); connect(client,SIGNAL(finished()),thread,SLOT(quit())); connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); if(config.typeView == CONSOLE) { connect(client,SIGNAL(println(QString)),(Console *)config.display,SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(int)),(Console *)config.display,SLOT(addClientToList(int))); } else if(config.typeView == WINDOW) { connect(client,SIGNAL(println(QString)),((MainWindow *)config.display),SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(int)),(MainWindow *)config.display,SLOT(addClientToList(int))); } connect(client,SIGNAL(removeClientFromList(int)),this,SLOT(removeMap(int))); SenseSystem::TechServer::client.insert(IDSocket,client); thread->start(); }
QTcpSocket:
/* * Run the thread */ void SenseSystem::Client::process(void) { clientSocket = new QTcpSocket(); // Trying to connect to socket descriptor if (!clientSocket->setSocketDescriptor(socketDescriptor)) { // Can't connect to socket descriptor finished(); } else { sql = new MainSQL("localhost","postirayka", "root","root"); // Initiolization settings for system timer = new QTimer(this); // setup signal and slot connect(timer,SIGNAL(timeout()),this,SLOT(readCommand())); // Reading command from MySQL timer->start(2000); // Initiolization settings for system autoriz = new QTimer(this); // setup signal and slot connect(autoriz,SIGNAL(timeout()),this,SLOT(finishThread())); // Reading command from MySQL autoriz->start(20000); // Initiolization settings for system timAlive = new QTimer(this); // setup signal and slot connect(timAlive,SIGNAL(timeout()),this,SLOT(finishThread())); // Reading command from MySQL timAlive->start(600000); timModem = new QTimer(this); // setup signal and slot connect(timModem,SIGNAL(timeout()),this,SLOT(readModem())); // Reading command from MySQL timModem->start(50000); timBufClear = new QTimer(this); // setup signal and slot connect(timBufClear,SIGNAL(timeout()),this,SLOT(bufferClear())); // Starting the thread of socket to execute events connect(clientSocket,SIGNAL(readyRead()),this, SLOT(socketReadClient())); connect(clientSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(clientChangeState())); connect(clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(socketErrorClient())); connect(clientSocket,SIGNAL(disconnected()),this, SLOT(socketRemoveClient())); // String to window QString strSocket; strSocket += QString("[") + QString::number(socketDescriptor) + "] New connection!!!" + "\r\n\r\n"; // Set text to the text browser println(strSocket); } }
Selected view:
// Checking if we need console application if(config.typeView == SenseSystem::CONSOLE) { QCoreApplication app(argc, argv); config.display = new Console(app.arguments()); // Return end of application return app.exec(); } // Checking if we need window application else { QApplication app(argc, argv); config.display = new MainWindow(); ((MainWindow *) config.display)->show(); // Return end of application return app.exec(); }
Console class:
/* * Constructor console task */ Console::Console(QStringList agruments,QObject *parent) : QObject(parent) { Console::agruments = agruments; // Function of fetch of user interface server = new SenseSystem::TechServer(config.port); connect(this,SIGNAL(println(QString)),this,SLOT(outStream(QString))); // Checking if server is started ? if(server != NULL) { // Set text to the text browser println("Server is working!\r\n"); println("UTC: "+QDateTime::currentDateTimeUtc().toString()+"\r\n"); } // If server isn't working else { // Set text to the text browser println("Server isn\'t working!!!\r\n"); println("Please check the internet connection\r\n"); } }
-
You didn't show socketReadClient() slot plus please use formatting this will make Your code more readable.
Check if config.display in both cases is correctly set, check if outStream() in Console is correctly set. -
socketReadClient()
- is specialized slot of handling packets./* * Recive of the request to the server */ void SenseSystem::Client::socketReadClient(void) { // Try to do something with the socket try { // Buffer read of client buffer.insert(buffer.size(),clientSocket->readAll());
But It's not necessary, in
Ubuntu
do not workingQThread
.
I'm do some changes:QThread *thread = new QThread(this); clientSocket->moveToThread(thread); Client *client = new Client(clientSocket,sql,IDSystem); client->moveToThread(thread); connect(thread,SIGNAL(started()),client,SLOT(process())); connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); connect(client,SIGNAL(finished()),thread,SLOT(quit())); connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); // Starting the thread of socket to execute events connect(clientSocket,SIGNAL(readyRead()),client, SLOT(socketReadClient())); connect(clientSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),client, SLOT(clientChangeState())); connect(clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),client, SLOT(socketErrorClient())); connect(clientSocket,SIGNAL(disconnected()),client, SLOT(socketRemoveClient())); if(config.typeView == CONSOLE) { connect(client,SIGNAL(println(QString)),((Console *)config.display),SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(qintptr)),((Console *)config.display),SLOT(addClientToList(qintptr))); } else if(config.typeView == WINDOW) { connect(client,SIGNAL(println(QString)),((MainWindow *)config.display),SLOT(outStream(QString))); connect(client,SIGNAL(addClientToList(qintptr)),((MainWindow *)config.display),SLOT(addClientToList(qintptr))); } connect(client,SIGNAL(removeClientFromList(qintptr)),this,SLOT(removeMap(qintptr))); SenseSystem::TechServer::client.insert(socketDescriptor,client); thread->start();
I can do this, because I have overloaded
incomingConnection(qintptr socketDescriptor)
function. But inUbuntu Console
modeQTcpSocket
in new thread do not recievereadyRead()
signal, this signal come in the main thread. But how do catchingreadyRead()
in new thread ????
InUbuntu Desktop
mode the code is worked !