class SS : public QObject
{
    Q_OBJECT
public:
    static QString getData(QString ip,int port,QString data)
    {
        QTcpSocket *m_pSocket=new QTcpSocket();
        
        m_pSocket->connectToHost(ip, port);
        
        if (!m_pSocket->waitForConnected())
        {
            return "noce";
        }
        //connect(m_pSocket, SIGNAL(connected()), &eventLoop, SLOT(quit()));
        //eventLoop.exec(); 
        if (m_pSocket->isOpen()==true)
        {
            int length = 0;
            
            m_pSocket->write(data.toLatin1(),data.length());
            m_pSocket->waitForBytesWritten(10000);
            QString datastr="";
            while (m_pSocket->waitForReadyRead(60000))
            {
                
                while(m_pSocket->bytesAvailable())
                {       
                    QString vTemp;
                    vTemp = m_pSocket->readLine();
                    datastr.append(vTemp);
                }
                
                
            }
            
            if (datastr!="")
                return datastr;
            
            if (m_pSocket->isOpen()==false)
            {
                m_pSocket->close();
            }
            return "err";
        }else{
            return "noconnect";
        }
        
    }
};
QTcpSocket 不通过事件,直接等待获取数据结果
点赞
收藏
 
 
 
 
 
 
 