QTcpSocket 不通过事件,直接等待获取数据结果

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";
		}
		
	}

};

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部