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

Cannot write with QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 5.6k 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.
  • N Offline
    N Offline
    noele1995
    wrote on last edited by
    #1

    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;
    

    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      when is socket_Send() called?

      I think you have a syncronization problem.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        noele1995
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          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?

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • N Offline
            N Offline
            noele1995
            wrote on last edited by
            #5

            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

            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