16,213
社区成员
发帖
与我相关
我的任务
分享
#ifndef MYSOCKET_H
#define MYSOCKET_H
#include <QTcpSocket>
class MySocket : public QTcpSocket
{
public:
MySocket();
private slots:
void readClient();
void wtf();
private:
void sentResult();
quint16 nextBlockSize;
};
#endif // MYSOCKET_H
#include <QtNetwork>
#include "mysocket.h"
MySocket::MySocket()
{
// connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
connect(this, SIGNAL(readyRead()), this, SLOT(wtf()));
nextBlockSize = 0;
}
void MySocket::wtf()
{
qDebug()<<"??";
QDataStream in(this);
in.setVersion(QDataStream::Qt_4_7);
if (nextBlockSize == 0) {
if (bytesAvailable() < sizeof(quint16))
return;
in >> nextBlockSize;
}
if (bytesAvailable() < nextBlockSize)
return;
QString temp;
in >> temp;
qDebug()<<temp;
QDataStream out(this);
out << quint16(0xFFFF);
close();
}
void MySocket::sentResult()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out.device()->seek(0);
out<<quint16(block.size()-sizeof(quint16));
write(block);
}