void server::sendFortune()
{
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out<<(quint16) 0;
out<<fortunes.at (qrand()%fortunes.size());
out.device()->seek(0);
out<<(quint16)(block.size)-sizeof(quint16);
QTcpSocket *clientConnection=tcpServer->nextPendingConnection();
connect(clientConnection,SIGNAL(disconnected(),clientConnectction,SLOT(deleterLater()));
clientConnect->write(block);
clientConnection->disconnectFromHost();
}
麻烦把上面每句的作用解释下
想深入了解如何工作的、、、
------解决方案--------------------
你这个是不是绑定在connected信号上?
如果是的话 就是这样解释:
void server::sendFortune()
{
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out<<(quint16) 0; //往block里面写入0(长度是quint16)
out<<fortunes.at (qrand()%fortunes.size()); //fortunes是LIST? 总之就是写入要传送的数据吧
out.device()->seek(0); //将指针挪回0(即block的开始位置)
out<<(quint16)(block.size)-sizeof(quint16); //把开始写的0 改成要传送的数据的大小
QTcpSocket *clientConnection=tcpServer->nextPendingConnection(); //获取连接进来的socket
connect(clientConnection,SIGNAL(disconnected(),clientConnectction,SLOT(deleterLater()));
clientConnect->write(block); //发送数据
clientConnection->disconnectFromHost(); //断开连接
}