qt-Network

tcpclient

waitForConnected() 等待链接的建立
waitForReadyRead() 等待新数据的到来
waitForBytesWritten() 等待数据写入socket
waitForDisconnected() 等待链接断开

qtcpsocket 不能跨线程使用

mp_socket_video = QSharedPointer<QTcpSocket>(new QTcpSocket(this));
connect(mp_socket_video.data(), SIGNAL(readyRead()), this, SLOT(readVideo()));
connect(mp_socket_video.data(), SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(videoSockdisplayError(QAbstractSocket::SocketError)));
connect(mp_socket_video.data(),SIGNAL(connected()),this,SLOT(videoSockConnected()));
connect(mp_socket_video.data(),SIGNAL(disconnected()),this,SLOT(videoSockDisconnected()));

void H264Video::videoSockConnected()
{
LOGFMTI("videos socket connected... send userID");
vector<unsigned char> cmdBuf;
m_protocol.sendUserID(g_initParam.userID, cmdBuf);
mp_socket_video->write((const char *)cmdBuf.data(),cmdBuf.size());
}
void H264Video::videoSockDisconnected()
{
LOGFMTI("videos socket disconnected...");
}
void H264Video::readVideo()
{
// 协议头是11bytes
if (mp_socket_video->bytesAvailable() < 11)
return;
QByteArray datas = mp_socket_video->read(11);
if (datas.at(0) != (char)0xa5)
{
assert(false);
// 协议头未找到
}
// 收到一个完整的 nalu 分组
datas = mp_socket_video->read(fine_len);
datas = mp_socket_video->readAll();
}


void H264Video::videoSockdisplayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(mp_socket_video->errorString()));
}

}