100 lines
2.4 KiB
C++
100 lines
2.4 KiB
C++
#include "mysocket.h"
|
|
|
|
#if _MSC_VER >= 1600
|
|
#pragma execution_character_set("utf-8")
|
|
#endif
|
|
|
|
|
|
MySocket::MySocket(qintptr sockDesc, QObject *parent): //sockDesc这玩意是一个描述套接字的东西营
|
|
QTcpSocket(parent),
|
|
m_sockDesc(sockDesc)
|
|
{
|
|
qRegisterMetaType<qintptr>("qintptr");
|
|
connect(this, SIGNAL(readyRead()),
|
|
this, SLOT(recvData()));//接收数据
|
|
connect(this, SIGNAL( error(QAbstractSocket::SocketError) ),
|
|
this, SLOT(SocketError(QAbstractSocket::SocketError)));//接收数据
|
|
}
|
|
|
|
MySocket::~MySocket()
|
|
{
|
|
|
|
}
|
|
|
|
void MySocket::SocketError(QAbstractSocket::SocketError)
|
|
{
|
|
int error = this->error();
|
|
switch (error)
|
|
{
|
|
case QAbstractSocket::RemoteHostClosedError:
|
|
{
|
|
qDebug()<<"RemoteHostClosedError";
|
|
emit disconnected();
|
|
break;
|
|
|
|
}
|
|
default:
|
|
{
|
|
qDebug()<<"SocketDefaultError";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void MySocket::sendData(QString IP, const QByteArray &data) //此处应修改为向某IP发送
|
|
{
|
|
//if (id == m_sockDesc && !data.isEmpty())
|
|
if (IP == peerAddress().toString().remove(0, 7) && !data.isEmpty())
|
|
{
|
|
this->write(data);
|
|
}
|
|
}
|
|
|
|
void MySocket::recvData(void)
|
|
{
|
|
QByteArray data = readAll();
|
|
|
|
if(data.contains("###%###"))//文件头处理
|
|
{
|
|
//qDebug()<<data;
|
|
//qDebug()<<"******************";
|
|
//qDebug()<<"head Start"<<QDateTime::currentDateTime().toString("ss:zzz");
|
|
fileName = QString(data).section("###%###", 0, 0);
|
|
fileType = QString(data).section("###%###", 1, 1);
|
|
fileSize = QString(data).section("###%###", 2, 2).toInt();
|
|
write("ok");//发送一个响应给客户机
|
|
qByteArray.clear();
|
|
waitForBytesWritten();//等待数据写入A
|
|
}
|
|
else
|
|
{
|
|
qByteArray.append(data);
|
|
//qDebug()<<qByteArray.length()<<data.length();
|
|
}
|
|
|
|
if(qByteArray.length()== fileSize)
|
|
{
|
|
write("ok");
|
|
if(fileType=="live")
|
|
{
|
|
QImage img;
|
|
img.loadFromData((qByteArray));
|
|
emit LiveViewIMG(m_sockDesc,img);
|
|
}
|
|
|
|
if(fileType=="save")
|
|
{
|
|
emit IMGdataReady(peerAddress().toString().remove(0, 7), qByteArray, fileName);
|
|
}
|
|
|
|
if(fileType=="once")
|
|
{
|
|
emit IMGdataReady(peerAddress().toString().remove(0, 7), qByteArray, fileName);
|
|
}
|
|
|
|
qByteArray.clear();
|
|
}
|
|
|
|
|
|
}
|