I looked for examples everywhere, and I just can't seem to find a solution.
Issue
So : I have this receiving class called DataReceiver. Basically, I just want to receive data from a TCP Socket on the localhost (127.0.0.1) network. It seems that I can write w/ this socket (I checked w/ the Wireshark software) but it looks like the program does not receive anything.
I placed a qDebug() statement in the onDataReceived QT SLOT. This slot does not seem to be triggered at any moment. I tried editing the mTcpSocket->readyRead() QT SIGNAL manually, and the slot is correctly called upon, which means that the signal is not emitted because the QTcpSocket doesn't receive anything (or at least I this so).
My computer is a Mac w/ macOS High Sierra. I scanned open ports, and found that 51470 & 50911 are open. Those are the ports I tested.
DataReceiver class
The code for this class is the following :
datareceiver.h :
class DataReceiver : QObject
{
Q_OBJECT
public:
// Con/Destructors
explicit DataReceiver(QObject *parent = nullptr);
explicit DataReceiver(const QString &pSourceAddress,
const unsigned int &pSourcePort,
QObject *parent = nullptr);
~DataReceiver();
// Network Management
bool connectToHost(void);
// Getters
QVector<float> *getData(void) const;
QTcpSocket *getTcpSocket(void) const;
QString getSourceAddress(void) const;
unsigned int getSourcePort(void) const;
// Setters
void setData(const QVector<float> *pData);
void setSourceAddress(const QString &pSourceAddress);
void setSourcePort(const unsigned int &pSourcePort);
signals:
public slots:
void onConnect();
void onDisconnect();
void onBytesWritten(qint64 bytes);
void onDataReceived();
private slots:
private:
void decodeData(const QByteArray &pMessage);
QTcpSocket *mTcpSocket;
QString mSourceAddress;
unsigned int mSourcePort;
const unsigned int mDataSize = 30;
QVector<float> *mData;
};
And the implementation : datareceiver.cpp
// Con/Destructors
DataReceiver::DataReceiver(QObject *parent) :
QObject(parent),
mTcpSocket(new QTcpSocket(this)),
mSourceAddress("127.0.0.1"),
mSourcePort(51470),
mData(new QVector<float>)
{
for( unsigned int i = 0; i < mDataSize; i++)
mData->append(.0);
}
DataReceiver::DataReceiver(const QString &pSourceAddress,
const unsigned int &pSourcePort,
QObject *parent) :
QObject(parent),
mTcpSocket(new QTcpSocket(this)),
mSourceAddress(pSourceAddress),
mSourcePort(pSourcePort),
mData(new QVector<float>)
{
for( unsigned int i = 0; i < mDataSize; i++)
mData->append(.0);
}
DataReceiver::~DataReceiver(){
mTcpSocket->disconnectFromHost();
mTcpSocket->waitForDisconnected();
delete mTcpSocket;
delete mData;
}
// Network Management
bool DataReceiver::connectToHost(void){
connect(mTcpSocket, SIGNAL(connected()), this, SLOT(onConnect()));
connect(mTcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
connect(mTcpSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
qDebug() << "connecting...";
//emit mTcpSocket->readyRead(); // For testing. We correctly trigger the onDataReceived slot
mTcpSocket->open(QAbstractSocket::ReadWrite);
mTcpSocket->connectToHost(getSourceAddress(), getSourcePort());
if(!mTcpSocket->waitForConnected(1000))
{
qDebug() << "Error: " << mTcpSocket->errorString();
return false;
}
mTcpSocket->write("Hello ?"); // Test
return true;
}
// Getters
QVector<float> *DataReceiver::getData(void) const{
return mData;
}
QTcpSocket *DataReceiver::getTcpSocket(void) const{
return mTcpSocket;
}
QString DataReceiver::getSourceAddress(void) const{
return mSourceAddress;
}
unsigned int DataReceiver::getSourcePort(void) const{
return mSourcePort;
}
// Setters
void DataReceiver::setData(const QVector<float> *pData){
// Not yet implemented
Q_UNUSED(pData);
}
void DataReceiver::setSourceAddress(const QString &pSourceAddress){
mSourceAddress = pSourceAddress;
}
void DataReceiver::setSourcePort(const unsigned int &pSourcePort){
mSourcePort = pSourcePort;
}
// Public Slots
void DataReceiver::onConnect(){
qDebug() << "connected...";
}
void DataReceiver::onDisconnect(){
qDebug() << "disconnected...";
}
void DataReceiver::onBytesWritten(qint64 bytes){
qDebug() << bytes << " bytes written...";
}
// Private Slots
void DataReceiver::onDataReceived(){
// Not yet implemented, code is for testing
qDebug() << "onDataReceived called !";
while(mTcpSocket->bytesAvailable()){
qInfo() << mTcpSocket->read(mTcpSocket->bytesAvailable());
qDebug() << mTcpSocket->readAll();
}
}
// Private Methods
void DataReceiver::decodeData(const QByteArray &pMessage){
// Not yet implemented
Q_UNUSED(pMessage);
}
The mData/mDataSize are here for future usage, and are thus to be ignored here.
Sender
To send data, I tried using netcat :
cat testfile.txt | nc 127.0.0.1 51470
I also created a DataSender class, w/ the same structure as the DataReceiver class. According to Wireshark, both methods seem to write data via TCP.
DataSender class
datasender.h
class DataSender : QObject
{
Q_OBJECT
public:
// Con/Destructors
explicit DataSender(QObject *parent = nullptr);
~DataSender();
// Network Management
bool connectToHost(void);
void sendData(void) const;
// Getters
QString getDestinationAddress(void) const;
unsigned int getDestinationPort(void) const;
// Setters
void setDestinationAddress(const QString &pDestinationAddress);
void setDestinationPort(const unsigned int &pDestinationPort);
signals:
public slots:
void onConnect();
void onDisconnect();
void onBytesWritten(qint64 bytes);
void onDataReceived();
private:
QTcpSocket *mTcpSocket;
QString mDestinationAddress;
unsigned int mDestinationPort;
};
datasender.cpp
DataSender::DataSender(QObject *parent) :
QObject(parent),
mTcpSocket(new QTcpSocket(this)),
mDestinationAddress("127.0.0.1"),
mDestinationPort(50911)
{
}
DataSender::~DataSender(){
mTcpSocket->disconnectFromHost();
mTcpSocket->waitForDisconnected();
delete mTcpSocket;
}
// Network Management
bool DataSender::connectToHost(void){
connect(mTcpSocket, SIGNAL(connected()), this, SLOT(onConnect()));
connect(mTcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
connect(mTcpSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
qDebug() << "connecting...";
mTcpSocket->setSocketOption(QAbstractSocket::KeepAliveOption, true);
mTcpSocket->connectToHost(getDestinationAddress(), getDestinationPort());
if(!mTcpSocket->waitForConnected(1000))
{
qDebug() << "Error: " << mTcpSocket->errorString();
return false;
}
return true;
}
void DataSender::sendData(void) const{
QByteArray lData("Hello, this is DataSender ! Do you copy ? I repeat, do you copy ?");
QByteArray lTemp;
QDataStream lData2(&lTemp, QIODevice::ReadWrite);
lData2 << lData.size();
if(mTcpSocket->state() == QAbstractSocket::ConnectedState)
{
mTcpSocket->write(lTemp); //write size of data
mTcpSocket->write(lData); //write the data itself
mTcpSocket->waitForBytesWritten();
}
}
// Getters
QString DataSender::getDestinationAddress(void) const{
return mDestinationAddress;
}
unsigned int DataSender::getDestinationPort(void) const{
return mDestinationPort;
}
// Setters
void DataSender::setDestinationAddress(const QString &pDestinationAddress){
mDestinationAddress = pDestinationAddress;
}
void DataSender::setDestinationPort(const unsigned int &pDestinationPort){
mDestinationPort = pDestinationPort;
}
// Public Slots
void DataSender::onConnect(){
qDebug() << "connected...";
}
void DataSender::onDisconnect(){
qDebug() << "disconnected...";
}
void DataSender::onBytesWritten(qint64 bytes){
qDebug() << bytes << " bytes written...";
}
void DataSender::onDataReceived(){
// Not yet implemented, code is for testing
qDebug() << "onDataReceived called !";
//while(mTcpSocket->bytesAvailable()){
//qInfo() << mTcpSocket->read(mTcpSocket->bytesAvailable());
//qDebug() << mTcpSocket->readAll();
//}
}
Client side Main
// Main routine ---------------------------------
int main(int argc, char **argv){
// Initializing application.
QApplication lApplication(argc, argv);
CLIENT::DataReceiver dataReceiver;
dataReceiver.connectToHost();
return lApplication.exec();
}
Server side Main
// Main routine ---------------------------------
int main(int argc, char **argv){
QApplication lApplication(argc, argv);
SERVER::DataSender lDataSender;
lDataSender.connectToHost();
lDataSender.sendData();
return lApplication.exec();
}
Behavior
Basically, when I run the DataReceiver class in the main, I get the following behavior :
connecting...
connected...
7 bytes written... # This is the write("Hello ?") I inserted just for testing
# "connected..." may occur after "7 bytes written...",
# I don't remember, I am not in front of my computer right now.
Conclusion
I believe I have simply forgotten something important, or that there is something I do not know of. This is a personal project, so a little exterior insight would be welcome !
Thank you very much !
Clovel
QHostAdress
not a string? – Mohammad A Kanan Mar 14 '18 at 0:17QLocalSocket
? – TheDarkKnight Mar 14 '18 at 9:04