
Originally Posted by
^NyAw^
Hi,
First of all, are you sure that you need threads for this?
No, it was just attempt to solve the problem this way.
If you comment "waitForReadyRead()" is normal that you get "num == 0" because you are not waiting to get data into the socket
I did step by step in debugger so that echo server that writes response immediately had enough time to write back. May be the reason was that until I'm in this function there are no events processed and bytesAvailable() relies on it.
Added after 4 minutes:

Originally Posted by
hugh_lou
Try to sleep some time before you start sending message to the server.
I did it with sleep and also saw in debugger that thread already started before I send data. It has no effect.
m_ClientSocket.start();
Sleep(10000);
bool bConnected = m_ClientSocket.connectToHost("localhost", 1000);
if (bConnected) m_ClientSocket.write("rtst", 4);
m_ClientSocket.start();
Sleep(10000);
bool bConnected = m_ClientSocket.connectToHost("localhost", 1000);
if (bConnected) m_ClientSocket.write("rtst", 4);
To copy to clipboard, switch view to plain text mode
Added after 10 minutes:
Once again last version of the source code:
ClientSocket
::ClientSocket() : QThread() {}
bool ClientSocket
::connectToHost(QString host,
int port
) { m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void ClientSocket::write(const char* Data,unsigned int nBytes) {
m_socket->write(Data);
m_socket->flush();
m_socket->waitForBytesWritten();
//qApp->processEvents();
//bool hasData=m_socket->waitForReadyRead();
//int num=m_socket->bytesAvailable();
//QByteArray data=m_socket->readAll();
//qDebug()<<num<<data;
}
}
void ClientSocket::test() {
qDebug
() <<
"I got something here!!!" <<
QThread::currentThreadId() ;
}
void ClientSocket::run() {
bool connected=connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()), Qt::QueuedConnection);
qDebug()<<connected;
//m_socket->moveToThread(this);
exec();
}
ClientSocket::ClientSocket() : QThread() {}
bool ClientSocket::connectToHost(QString host, int port) {
m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void ClientSocket::write(const char* Data,unsigned int nBytes) {
if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState)) {
m_socket->write(Data);
m_socket->flush();
m_socket->waitForBytesWritten();
//qApp->processEvents();
//bool hasData=m_socket->waitForReadyRead();
//int num=m_socket->bytesAvailable();
//QByteArray data=m_socket->readAll();
//qDebug()<<num<<data;
}
}
void ClientSocket::test() {
qDebug() << "I got something here!!!" << QThread::currentThreadId() ;
}
void ClientSocket::run() {
m_socket=new QTcpSocket();
bool connected=connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()), Qt::QueuedConnection);
qDebug()<<connected;
//m_socket->moveToThread(this);
exec();
}
To copy to clipboard, switch view to plain text mode
Usage:
SomeClass::SomeClass() : m_ClientSocket() {
m_ClientSocket.start();
Sleep(10000);
bool bConnected = m_ClientSocket.connectToHost("localhost", 1000);
if (bConnected) {
m_ClientSocket.write("rtst", 4);
}
}
SomeClass::SomeClass() : m_ClientSocket() {
m_ClientSocket.start();
Sleep(10000);
bool bConnected = m_ClientSocket.connectToHost("localhost", 1000);
if (bConnected) {
m_ClientSocket.write("rtst", 4);
}
}
To copy to clipboard, switch view to plain text mode
@^NyAw^: I also tried the same code without QThread and run(), just QObject as parent. The same strange effect of readyRead() absence.
Added after 15 minutes:
I just tested it inside my echo server app (simple Qt application) an it works! (Talker is the echo server). test() slot gets called. It should has something to do with DLL/EventLoop, I guess...
int main(int argc, char *argv[])
{
Talker* t=new Talker();
ClientSocket* c=new ClientSocket();
c->start();
Sleep(10000);
bool bConnected = c->connectToHost("localhost", 1000);
if (bConnected)
{
c->write("rtst", 4);
}
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Talker* t=new Talker();
ClientSocket* c=new ClientSocket();
c->start();
Sleep(10000);
bool bConnected = c->connectToHost("localhost", 1000);
if (bConnected)
{
c->write("rtst", 4);
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks