Problem with QTcpSocket
-
Hi all,
I'm upgrading to Qt an old application (initially implemented in Builder 3) that implemented a TCP/IP client which connected to a server (running in the same local machine) implemented in Tcl.
This client (once connected, of course) sends to the server a number of messages containing names of files to be somehow processed, and, upon termination, the server sends back to the client a message with a termination code indicating the status of the operation ("correct" or "incorrect"). When all the files have been processed, the client send to the server a message indicating to close the communication and terminate.
Besides the Qt help, I've been studying lots of examples, and I've implemented this:Client (version 1)
void MyDesign::ProcessFiles() { QString ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); QTcpSocket socket(this); socket.connectToHost(ipAddress, 1700, QIODevice::ReadWrite); if (!socket.waitForConnected(-1)) { QMessageBox::critical(this, "Error", socket.errorString()); return; } // Process the files in the list for (int i = 0; (i < FileList.size()); i++) { QString filename = FileList.at(i); socket.write(filename.toAscii()); socket.flush(); if (!socket.waitForReadyRead(-1)) { QMessageBox::critical(this, "Error", socket.errorString()); return; } QByteArray data_in = socket.readAll(); // If a processing error occurred if (data_in.at(0) != '0') { QMessageBox::critical(this, "Error", "Unable to process file " + filename); return; } // Other processing... } // Terminate socket.write("2"); // Code for "terminate" socket.flush(); socket.close(); socket.waitForDisconnected(-1); }
Well, the problem is that, although apparently the client sends a message (I've read the value returned by write, and matches exactly with the length of the message), the server does not receive anything, not even the first message from the client; so, the server cannot process the file sent (nor of course, send the termination code back), and thus the client hangs when waiting for ReadyRead signal.
I've implemented another client in Tcl, and the server works ok, so the problem must be in the C++ client. The most curious thing is that I've modified the handshake so that the server sends an acknowledge message to the client just upon accepting the connection. The new client is then like this:
Client (version 2)void MyDesign::ProcessFiles() { QString ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); QTcpSocket socket(this); socket.connectToHost(ipAddress, 1700, QIODevice::ReadWrite); if (!socket.waitForConnected(-1)) { QMessageBox::critical(this, "Error", socket.errorString()); return; } if (!socket.waitForReadyRead(-1)) { QMessageBox::critical(this, "Error", socket.errorString()); return; } QByteArray data_in = socket.readAll(); // If a connection error occurred if (data_in.at(0) != '0') { QMessageBox::critical(this, "Error", "Unable to connect to server"); return; } // Process the files in the list for (int i = 0; (i < FileList.size()); i++) { QString filename = FileList.at(i); socket.write(filename.toAscii()); socket.flush(); if (!socket.waitForReadyRead(-1)) { QMessageBox::critical(this, "Error", socket.errorString()); return; } data_in = socket.readAll(); // If a processing error occurred if (data_in.at(0) != '0') { QMessageBox::critical(this, "Error", "Unable to process file " + filename); return; } // Other processing... } // Terminate socket.write("2"); // Code for "terminate" socket.flush(); socket.close(); socket.waitForDisconnected(-1) }
Surprisingly (for me) the server does send the right message, and client does receive it!, and it hangs exactly in the same place than in version 1. So, what's wrong with the writing part of my client's code?
Thanks,
Regards,
-
Hi @JCBaraza,
I hope you are well aware that the
waitForReady...
functions can only be used in threads.If you don't have a thread (beside the main thread), you must use signals&slots, like the Fortune Client example.
-
@aha_1980 said in Problem with QTcpSocket:
Hi @JCBaraza,
I hope you are well aware that the
waitForReady...
functions can only be used in threads.If you don't have a thread (beside the main thread), you must use signals&slots, like the Fortune Client example.
Hi aha1980,
Thanks a lot for your quick answer. No, I'm affraid I didn't know about that. When I saw the Qt examples with sockets, I rapidly discarded using signals and slots because this kind of programming is a bit puzzling to me (just have a look to my code ;-) ). Well, I'll have to revisit Qt examples and get familiar to signals and slots outside the main window of my application.
Regards,