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. QTcpSocket
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 3.9k 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.
  • L Offline
    L Offline
    luca72
    wrote on last edited by
    #1

    Hello i have a simple socket:
    @
    socket_base = new QTcpSocket;
    connect(socket_base,SIGNAL(connected()),this,SLOT(risultato_connessione()));
    connect(socket_base,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_errore()));
    connect(socket_base,SIGNAL(readyRead()),this,SLOT(leggo_base()));@

    The second connect
    @connect(socket_base,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_errore()));@

    Crash the program i get
    The program has unexpectedly finished.
    if i delete the connect it works, can you help me tu understand why

    Thanks

    Luca

    1 Reply Last reply
    0
    • F Offline
      F Offline
      favoritas37
      wrote on last edited by
      #2

      I would guess that one of the slot functions you define in the connect function gets called after an emitting of a signal and crashes there in the body of the function. That is because your above lines are fine, though i would try to mach the arguments of the signal to the one of the slot function.

      Also, when it crashes what is the console output? Can you run it in debug mode to see exactly where it crashes?

      If you don't have a debugger follow the old fashioned way to print messages to the output at the beginning of every slot function to see at least where the problem lιes.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca72
        wrote on last edited by
        #3

        i get segmentation fault

        1 Reply Last reply
        0
        • F Offline
          F Offline
          favoritas37
          wrote on last edited by
          #4

          You get segmentation in which function? Which line of code?
          Do you use pointers that are not yet initialized?

          If you don't provide more information it will be impossible for us to help.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca72
            wrote on last edited by
            #5

            The function is this:

            @void Widget::connetto(){
            using namespace std;
            string lettura;
            QString lettura_qstring;
            QStringList lista;
            controllo_reg(registrato);
            if (registrato == false){
            user = ui->lineEdit->text();
            passw = ui->lineEdit_2->text();
            email = ui->lineEdit_3->text();
            if (user == "" && passw =="" && email == ""){
            QMessageBox messaggio;
            messaggio.information(this, tr("MondoGames"),tr("Devi Prima Registrarti Scemo\n""Completa i campi e poi riconnettiti e registrati"),messaggio.Ok);
            }
            else {
            socket_base = new QTcpSocket;

                    connect(socket_base,SIGNAL(connected()),this,SLOT(risultato_connessione()));
                    connect(socket_base,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_errore()));
                    connect(socket_base,SIGNAL(readyRead()),this,SLOT(leggo_base()));
            
                    socket_base->connectToHost("irc.explosionirc.net", 6667);
                    ui->label->setText("Connessione in corso");
                }
            
            }
            else {
                ifstream fin("flrg");
                getline(fin, lettura);
                fin.close();
                lettura_qstring = QString::fromStdString(lettura);
                lista = lettura_qstring.split(" ");
                user = lista[0];
                passw = lista[1];
                email = lista[2];
                socket_base->connectToHost("irc.explosionirc.net", 6667);
                ui->label->setText("Connessione in corso");
            }
            

            }@

            I get segfault here:
            @socket_base->connectToHost("irc.explosionirc.net", 6667);@

            but if i delete:
            @connect(socket_base,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_errore()));@

            i have no error

            Thanks

            Luca

            1 Reply Last reply
            0
            • F Offline
              F Offline
              favoritas37
              wrote on last edited by
              #6

              Can you please post the code of socket_errore function?

              From my point of view, you try to connect to irc.explosionirc.net:6667 unsuccessfully, it emits the signal reporting the error and there must be something in socket_errore function that causes the problem.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca72
                wrote on last edited by
                #7

                Hello
                Here's the code, thanks for your help

                @void Widget::socket_errore(){
                using namespace std;
                string str_errore;
                QString errore;
                QMessageBox messaggio;
                errore = socket_base->errorString();
                str_errore = errore.toStdString();
                messaggio.information(this,tr("MondoGames"),tr(str_errore.c_str()),messaggio.Ok);
                }@

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  favoritas37
                  wrote on last edited by
                  #8

                  Why don't you try something simpler like this:

                  @
                  void Widget::socket_errore()
                  {
                  QString title = tr("MondoGames");
                  QString text = tr( socket_base->errorString());

                      QMessageBox::information(this,title,text);
                  

                  }
                  @

                  Also be sure that the tr() functions return what you want.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #9

                    If your original if statement
                    @
                    if (registrato == false){
                    @
                    falls through to the "else", then it looks like socket_base never gets initialized. (Which happens in line 16.)

                    Unless it's initialized somewhere else, then that's probably why it would segfault at line 36.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luca72
                      wrote on last edited by
                      #10

                      Thanks mlong you are right

                      Luca

                      1 Reply Last reply
                      0

                      • Login

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