0

I am developing an TCP server application. I have the newDataReceived slot and I emit a signal in it like this:

void myclass::newDataReceived()
{

    char data_received[1024] = {0};
    client->read(data_received, client->bytesAvailable());
    QString msg = data_received;
    QString client_ip = client->peerAddress().toString();

    emit dataReceived(msg,client_ip);
}

I have catched the signal from MainWindow, there is no problem. But, I have another class which is a QThread and I want this class to catch this signal too. But it does not do it. I connected the signal to my slot like,

srv_thread = new myclass();
connect(srv_thread, SIGNAL(dataReceived(QString,QString)), this, SLOT(incoming_message(QString,QString)));

What am I missing?

Thanks in advance!

Venemo
  • 18,515
  • 13
  • 84
  • 125
thehilmisu
  • 342
  • 1
  • 5
  • 13
  • 1
    You should only have one `myclass`. Is that the case? 'Cause it looks like you're making a new one when you connect with the thread... – Xavier Holt Dec 26 '12 at 13:40
  • i have, myclass MainWindow and Thread classes. myclass emits dataReceived signal. I can catch this signal from MainWindow but i could not catch it from my thread class. That is the case. – thehilmisu Dec 26 '12 at 13:48
  • 1
    What I mean is that you should only have one instance of `myclass` - the line `xxx = new myclass();` should only appear once in your code. Can you show where you connect that signal to `MainWindow` so I can get a better idea of what you're doing there? – Xavier Holt Dec 26 '12 at 14:03
  • Can you show the code of your `QThread` derived class ? – alexisdm Dec 26 '12 at 15:16
  • @XavierHolt you were right. I have been making new one. Thanks. – thehilmisu Dec 26 '12 at 15:21
  • In this code you have at least 2 Undefined Behaviors. 1. `client->bytesAvailable()` can be larger than 1024. 2. You are assuming that you are receiving data in predefined chunks (take a look on [this answear](https://stackoverflow.com/a/19682690/1387438)). 3. Your conversion to `QString` is naive and may lead to buffer overflow (you do not have warranty of null termination of buffer). – Marek R Jan 01 '18 at 11:43

0 Answers0