QTcpSocket



  • 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



  • 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.



  • i get segmentation fault



  • 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.



  • 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



  • 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.



  • 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);
    }@



  • 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.


  • Moderators

    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.



  • Thanks mlong you are right

    Luca


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.