Cannot write with QTcpSocket



  • The problem is in the send data function, when i do isWritable it retuns false, when i call write(d2) it returns -1, and when i try to flush it crashes with segmentation fault. Also i get this console output. Can it be for using more than 1 thread? how should i fix it? thanks
    @QSocketNotifier: socket notifiers cannot be disabled from another thread
    QSocketNotifier: Multiple socket notifiers for same socket 560 and type Read
    (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
    QIODevice::write: ReadOnly device@

    @
    //in the header...
    typedef struct {QString name;QString pcName;QString userName;QString admin;QString flagCode;QString IP;QTcpSocket *sck;}client;
    QList<client *> Clients;

    fMain::fMain(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::fMain)
    {
    ui->setupUi(this);

    for(int i = 0;i < 3;i++)
    {
        newConexPorts[i] = 1181 + i;
        newConexServers[i] = new QTcpServer(this);
    }
    
        //SOCKETS
    connect(this,SIGNAL(signalRefreshConexList()),this,SLOT(slotRefreshConexList()));
    for(int i = 0;i < 3;i++)
    {
        connect(newConexServers[i],SIGNAL(newConnection()),this,SLOT(incomingConnection()));
    }
    

    }

    void fMain::socket_Send(client *Client,const QString &d)
    {
    QByteArray d2;
    d2.append(d);
    bool _a = Client->sck->isWritable();
    qint64 bytes = Client->sck->write(d2);
    Client->sck->flush();
    Client->sck->waitForBytesWritten();
    }

    void fMain::incomingConnection(void)
    {
    QTcpServer *SERVER = (QTcpServer *)QObject::sender();

    if(SERVER->hasPendingConnections())
    {
        QTcpSocket *SOCKET = SERVER->nextPendingConnection();
    
        //thread
        newConexThread *thread = new newConexThread;
        thread->setThreadData(SOCKET,this);
        connect(thread,SIGNAL(signalRefreshConexList()),this,SLOT(slotRefreshConexList()));
        connect(thread,SIGNAL(signalSocketNotLogged(QTcpSocket*)),this,SLOT(slotDeleteSocket(QTcpSocket*)));
        thread->start();
    }
    

    }

    void fMain::slotDeleteSocket(QTcpSocket *s)
    {
    s->close();
    s->deleteLater();
    }

    void fMain::slotDeleteSocket2(void)
    {
    QTcpSocket *SOCKET = (QTcpSocket *)QObject::sender();

    if(!Clients.empty())
    {
        foreach (client *Client, Clients)
        {
            if(Client->sck == SOCKET)
            {
                Clients.removeOne(Client);
            }
        }
    }
    
    SOCKET->close();
    SOCKET->deleteLater();
    
    emit signalRefreshConexList();
    

    }

    void fMain::slotSocketReadyRead(void)
    {
    QTcpSocket *SOCKET = (QTcpSocket *)QObject::sender();
    QString dataReceived = QString(SOCKET->readAll());
    QStringList listData = dataReceived.split("|");
    client *client_sender;

    //code...
    }

    //NEW_CONEX_THREAD

    void newConexThread::setThreadData(QTcpSocket *s,fMain *f)
    {
    SOCKET = s;
    frm = f;
    }

    void newConexThread::run()
    {
    if(SOCKET->state() != 3)
    {
    SOCKET->waitForConnected();
    }
    if(SOCKET->state() == 3)
    {
    if(SOCKET->waitForReadyRead())
    {
    QString dataReceived = QString(SOCKET->readAll());
    QStringList listData = dataReceived.split("|");

            if(listData.at(0) == NEW_CONEX_ID)
            {
                if (!(listData.length() < 7))
                {
                    if (listData.at(6) == frm->password)
                    {
                        connect(SOCKET,SIGNAL(disconnected()),frm,SLOT(slotDeleteSocket2()));
    
                        client *Client = new client;
    
                        Client->name = listData.at(1);
    
                        Client->IP = SOCKET->peerAddress().toString();
    
                        Client->sck = SOCKET;
    
                        frm->Clients.append(Client);
    
                        connect(SOCKET,SIGNAL(disconnected()),frm,SLOT(slotDeleteSocket2()));
                        connect(SOCKET,SIGNAL(readyRead()),frm,SLOT(slotSocketReadyRead()));
    
                        emit signalRefreshConexList();
                        this->deleteLater();
                        return;
                    }
                }
            }
        }
    }
    emit signalSocketNotLogged(SOCKET);
    this->deleteLater();
    return;
    

    }@



  • Hi,

    when is socket_Send() called?

    I think you have a syncronization problem.



  • It is called on a slot of a button but the socket isnt reading or doing anything else, just waiting.



  • Hi,

    what I see is that you set Client->sck only after receiving something; are you sure your socket field are set when you call send?



  • Thats ok its for loggin the socket, i solved the problem, it was an error on calling the function, the pointer that i was passing was null. Sorry and thanks for yout time


Log in to reply
 

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