Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Read data different socket (QTcpScoket) without thread



  • Hi,
    anyone can help, i have trouble about qtcpsocket

    2 device connected to my application with different ip
    device1 : 192.168.200.254
    device2 : 192.168.200.253
    after write command to device1 this app can read the response from device1, then i'm write command to device2 this app cant read the response from device2 but in wireshark i can see the response.

    here is my snippet code file : manageserver.cpp

    void ManageServer::startManageServer(){
        QHostAddress ip_address{QHostAddress::Any};
        quint16 manage_port{16385};
        if(!this->listen(ip_address,manage_port)){
            qDebug()<<QString("manageserver could not start - %1").arg(this->errorString());
        }else{
            qInfo()<<"manageserver start at port: "<<QString::number(manage_port);
            connect(this,SIGNAL(setSocketIDs(qintptr)),this,SLOT(getSocketIDs(qintptr)));
            connect(this,SIGNAL(setActiveDeviceNumber(QString)),this,SLOT(getActiveDeviceNumber(QString)));
        }
    }
    
    void ManageServer::incomingConnection(qintptr socketDesc)
    {
        manageserver_socks = new QTcpSocket();
        if( manageserver_socks->setSocketDescriptor(socketDesc) ){
    
            connect(manageserver_socks,SIGNAL(readyRead()),this,SLOT(readyRead()));
            connect(manageserver_socks,SIGNAL(disconnected()),this,SLOT(disconnected()));
    
            manageserver_socks->write("Welecome To WDServer");
            manageserver_socks->flush();
            //pair socket with peer address
            connected_clients.insert(manageserver_socks->peerAddress(),manageserver_socks);
            //set socketIds
            setSocketIDs(socketDesc);
            manageserver_socks->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
    
        }
    
        qDebug()<< socketDesc<< "client connected to manageserver";
    }
    
    void ManageServer::readyRead()
    {
        QByteArray Data = manageserver_socks->readLine();
        
    }
    
    void ManageServer::manageServerSendCommand(QString ip_address,QByteArray command)
    {
        QHostAddress ipaddress = QHostAddress(ip_address);
        if(((QTcpSocket*)connected_clients.value(ipaddress))->isOpen()){
    
            ((QTcpSocket*)connected_clients.value(ipaddress))->write(command);
            ((QTcpSocket*)connected_clients.value(ipaddress))->flush();
    
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("ManageServer");
            msgBox.setText("Device not ready.");
            msgBox.exec();
        }
    }
    
    void ManageServer::disconnected()
    {
        connected_clients.remove(manageserver_socks->peerAddress());
    }
    


  • @rifky said in Read data different socket (QTcpScoket) without thread:

    void ManageServer::readyRead()
    {
    QByteArray Data = manageserver_socks->readLine();

    }

    You only have one instance of ManageServer, right? And manageserver_socks is a member variable, set in incomingConnection(), right? So how does your readyRead() read from different sockets, it seems only to read from the last manageserver_socks created? (Similar issue in your disconnected() too.) Unless I'm missing something....



  • it's solved i'm forked this code , it's support multi instance of socket


Log in to reply