Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP Socket problem
Qt 6.11 is out! See what's new in the release blog

TCP Socket problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dan3460D Offline
    Dan3460D Offline
    Dan3460
    wrote on last edited by
    #1

    Working on a program that gets information from a telnet connection to a web server. Works fine unless I lose connection or the web server was not ready.
    To solve this i created a button a dialog and this dialog calls at the parent function to connect. I tested this button with just a message on the connect function and works fine. When i put the code to connect i get a segmentation error at connectToHost() function of the socket.
    Here is the code on the main window:
    *

    • bool PowerMonitor::connectToServer()
      {
      socket->connectToHost(data->getHostIp(),data->getPort());
      if(socket->waitForConnected(3000)==true)
      {
      data->setSystemConnected(true); // Need to add handling for fail connection
      ui->statusBar->showMessage("Connected to:"+data->getHostIp()+" Port:"+QString::number(data->getPort()));
      }
      else
      {
      data->setSystemConnected(false);
      QMessageBox::warning(this,"Connection","Could not find Sensors");
      ui->statusBar->showMessage("No Connection");
      }
      return true;
      }*

    This is the code from the Child Dialog:

    • void SystemDataDisplay::on_reconnect_clicked()
      {
      PowerMonitor pmonitor = qobject_cast<PowerMonitor>(parent());
      pmonitor->connectToServer();
      }

    My hunch is something to do with calling connectToHost() function for the second time. Any ideas?
    Thanks for your help

    1 Reply Last reply
    0
    • Dan3460D Offline
      Dan3460D Offline
      Dan3460
      wrote on last edited by
      #2

      After running using the debugger, i understand what is going on.
      When i'm creating the QDialog object from the main program I was using the following :

      • SystemDataDisplay systemdatadisplay;
        systemdatadisplay.setData(data);
        systemdatadisplay.exec();

      Thus, i was not passing the address of the parent to the child. This was solved like this:
      SystemDataDisplay systemdatadisplay(this);
      systemdatadisplay.setData(data);
      systemdatadisplay.exec();

      No it works fine

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Glad you found out but that design is pretty bad. You are creating a tight coupling where none is needed.

        If you want that dialog to trigger something in your other widget, then just give that dialog a signal and connect it before calling exec.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved