1class SS : public QObject 2{ 3 Q_OBJECT 4public: 5 static QString getData(QString ip,int port,QString data) 6 { 7 QTcpSocket *m_pSocket=new QTcpSocket(); 8 9 m_pSocket->connectToHost(ip, port); 10 11 if (!m_pSocket->waitForConnected()) 12 { 13 return "noce"; 14 } 15 //connect(m_pSocket, SIGNAL(connected()), &eventLoop, SLOT(quit())); 16 //eventLoop.exec(); 17 if (m_pSocket->isOpen()==true) 18 { 19 int length = 0; 20 21 m_pSocket->write(data.toLatin1(),data.length()); 22 m_pSocket->waitForBytesWritten(10000); 23 QString datastr=""; 24 while (m_pSocket->waitForReadyRead(60000)) 25 { 26 27 while(m_pSocket->bytesAvailable()) 28 { 29 QString vTemp; 30 vTemp = m_pSocket->readLine(); 31 datastr.append(vTemp); 32 } 33 34 35 } 36 37 if (datastr!="") 38 return datastr; 39 40 41 if (m_pSocket->isOpen()==false) 42 { 43 44 m_pSocket->close(); 45 } 46 return "err"; 47 }else{ 48 return "noconnect"; 49 } 50 51 } 52 53};
QTcpSocket 不通过事件,直接等待获取数据结果
Stella981
2021-10-12
945 0 0
点赞
收藏
评论区
加载中...