I figured it might be helpful to post the code. I've cut out most everything but a basic configuration.
I use netcat to capture the output and provide the server side socket. I'm piping data into netcat to trigger my client side socket write:
here's the command I use: nc -l -s 127.0.0.1 -p 7496 < zeros.txt
zeros.txt is a 60KB file filled with ascii 0's.
I'm using the 4.6.2 and this problem occurs on both Windows (qt creator/netcat with cygwin) and Linux.
Here's the header:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QtCore>
#include <QTcpSocket>
{
Q_OBJECT
public:
Thread();
// Thread entry point
void run(void);
qint32 tx(void);
public slots:
void multiplexIO(void);
private:
};
#endif
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QtCore>
#include <QTcpSocket>
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
// Thread entry point
void run(void);
qint32 tx(void);
public slots:
void multiplexIO(void);
private:
QTcpSocket* mp_Socket;
};
#endif
To copy to clipboard, switch view to plain text mode
Here's the src:
#include "interface.h"
int reqData=1;
///////////////////////////////////////////////////////////////
Thread::Thread()
{
start();
}
void Thread::multiplexIO(void)
{
if(reqData)
{
qDebug() << "making request";
reqData = 0;
tx();
}
}
void Thread::run(void)
{
qDebug() << "thread started:" << currentThreadId();
// Create TCP interface socket
mp_Socket->connectToHost("127.0.0.1", 7496);
// Wait for connection to be established
if (!mp_Socket->waitForConnected(1000))
{
qDebug() << "Connect timed out: " << mp_Socket->errorString();
return;
}
// Connect our socket read routine to our signal that new data has arrived
//connect(mp_Socket, SIGNAL(readyRead()), this, SLOT(multiplexIO()));
connect(mp_Socket, SIGNAL(readyRead()), this, SLOT(multiplexIO()), Qt::QueuedConnection);
// Force the first call to check our socket
// multiplexIO();
// Start thread's event loop
exec();
qDebug() << "thread terminated";
}
qint32 Thread::tx()
{
qDebug() << "Making data request!";
if(!mp_Socket)
{
return -1;
}
mp_Socket->write("hello\n", 6);
mp_Socket->flush();
mp_Socket->waitForBytesWritten(10000);
// mp_Socket->disconnectFromHost();
return 0;
}
//////////////////
// Main
//////////////////
int main(int argc, char *argv[])
{
Thread thread;
core.exec();
}
#include "interface.h"
int reqData=1;
///////////////////////////////////////////////////////////////
Thread::Thread()
{
start();
}
void Thread::multiplexIO(void)
{
if(reqData)
{
qDebug() << "making request";
reqData = 0;
tx();
}
}
void Thread::run(void)
{
qDebug() << "thread started:" << currentThreadId();
// Create TCP interface socket
mp_Socket = new QTcpSocket;
mp_Socket->connectToHost("127.0.0.1", 7496);
// Wait for connection to be established
if (!mp_Socket->waitForConnected(1000))
{
qDebug() << "Connect timed out: " << mp_Socket->errorString();
return;
}
// Connect our socket read routine to our signal that new data has arrived
//connect(mp_Socket, SIGNAL(readyRead()), this, SLOT(multiplexIO()));
connect(mp_Socket, SIGNAL(readyRead()), this, SLOT(multiplexIO()), Qt::QueuedConnection);
// Force the first call to check our socket
// multiplexIO();
// Start thread's event loop
exec();
qDebug() << "thread terminated";
}
qint32 Thread::tx()
{
qDebug() << "Making data request!";
if(!mp_Socket)
{
return -1;
}
mp_Socket->write("hello\n", 6);
mp_Socket->flush();
mp_Socket->waitForBytesWritten(10000);
// mp_Socket->disconnectFromHost();
return 0;
}
//////////////////
// Main
//////////////////
int main(int argc, char *argv[])
{
QCoreApplication core(argc, argv);
Thread thread;
core.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks