其他分享
首页 > 其他分享> > QTcpServer QTcpSoket 实现文件更新

QTcpServer QTcpSoket 实现文件更新

作者:互联网

服务端写文件:

#include "UpdateServer.h"

UpdateServer::UpdateServer(QWidget *parent)
: QWidget(parent), m_filesize(0), m_socket(nullptr),
m_currentWritten(0), m_blockSize(1024*1024 *1)
{
ui.setupUi(this);
findFile();
m_tcpServer.listen(QHostAddress::Any, 10829);
connect(&m_tcpServer, &QTcpServer::newConnection, this, &UpdateServer::newConnection);
}

UpdateServer::~UpdateServer()
{
if (m_socket!=nullptr)
delete m_socket;
delete serverIP;
}
void UpdateServer::findFile()
{
QDir dir("update");
QStringList fileList = dir.entryList();
ui.listWidget->clear();
m_filequeue.clear();
for each (QString fname in fileList)
{
if (fname == "." | fname == "..") continue;
ui.listWidget->addItem(fname);
m_filequeue.enqueue(fname);
}
}
void UpdateServer::newConnection()
{
m_socket = m_tcpServer.nextPendingConnection();
//connect(m_socket, &QTcpSocket::disconnected, m_socket, &QObject::deleteLater);
connect(m_socket, &QIODevice::bytesWritten, this, &UpdateServer::onBytesWritten);
connect(m_socket, &QIODevice::readyRead, this, &UpdateServer::onReadyRead);
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
m_outstream.setDevice(m_socket);
m_outstream.setVersion(QDataStream::Qt_5_0);
}
void UpdateServer::onReadyRead()
{
if (m_filequeue.isEmpty())
findFile();
sendF();
}
void UpdateServer::sendF()
{
m_file.setFileName("update\\" + m_filequeue.dequeue());
if (!m_file.open(QIODevice::ReadOnly)) {
qCritical() << m_file.errorString();
QMessageBox::critical(this, QStringLiteral("错误!"), m_file.errorString());
return;
}
m_filesize = m_file.size();
QFileInfo info(m_file.fileName());
QString fname = info.fileName();
qDebug() << "write bytes:filesize=" << m_filesize << ",filename=" << fname;
m_outstream << m_filesize << fname;
QByteArray arry = m_file.read(m_blockSize);
int size = arry.size();
qDebug() << "arry size()=" << size;
m_outstream.writeRawData(arry.constData(), size);
}
void UpdateServer::onBytesWritten(const qint64 & bytes)
{

Q_UNUSED(bytes)
QByteArray arry = m_file.read(m_blockSize);
if (arry.isEmpty()) {
reset();
return;
}
int size = arry.size();
qDebug() << "bytes write size=" << size;
m_outstream.writeRawData(arry.constData(), size);
}
void UpdateServer::reset()
{
m_file.close();
m_currentWritten = 0;
if (m_filequeue.isEmpty())
{
m_socket->close();
}
else
sendF();
}
void UpdateServer::setHost(QString host)
{
if (m_socket != nullptr)
m_socket->connectToHost(host, 10829);
}

void UpdateServer::onSocketStateChanged(QAbstractSocket::SocketState state)
{
switch (state) {
case QAbstractSocket::UnconnectedState:
qDebug() << __FUNCTION__ << "QAbstractSocket::UnconnectedState";
break;
case QAbstractSocket::HostLookupState:
qDebug() << __FUNCTION__ << "QAbstractSocket::HostLookupState";
break;
case QAbstractSocket::ConnectingState:
qDebug() << __FUNCTION__ << "QAbstractSocket::ConnectingState";
break;
case QAbstractSocket::ConnectedState:
qDebug() << __FUNCTION__ << "QAbstractSocket::ConnectedState";
sendF();
break;
case QAbstractSocket::BoundState:
break;
case QAbstractSocket::ClosingState:
qDebug() << __FUNCTION__ << "QAbstractSocket::ClosingState";
break;
case QAbstractSocket::ListeningState:
break;
default:
break;
}
}

void UpdateServer::onSocketError(QAbstractSocket::SocketError error)
{
switch (error) {
case QAbstractSocket::ConnectionRefusedError:
qDebug() << __FUNCTION__ << "QAbstractSocket::ConnectionRefusedError";
break;
case QAbstractSocket::RemoteHostClosedError:
qDebug() << __FUNCTION__ << "QAbstractSocket::RemoteHostClosedError";
break;
case QAbstractSocket::HostNotFoundError:
qDebug() << __FUNCTION__ << "QAbstractSocket::HostNotFoundError";
break;
case QAbstractSocket::SocketTimeoutError:
qDebug() << __FUNCTION__ << "QAbstractSocket::SocketTimeoutError";
break;
case QAbstractSocket::AddressInUseError:
qDebug() << __FUNCTION__ << "QAbstractSocket::AddressInUseError";
break;
default:
break;
}
}

 

客户端:

#include "ChkUpdate.h"

ChkUpdate::ChkUpdate(QObject *parent)
: QObject(parent), m_socket(nullptr), m_fileSize(0),
m_fileByteReceived(0), m_blockSize(1024*1024*1)
{
m_socket = new QTcpSocket(this);
serverIP = new QHostAddress();
if (!serverIP->setAddress("159.75.209.42"))
QMessageBox::about(0, "提示!", "连接失败!");
m_socket->connectToHost(*serverIP, 10829);
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
connect(m_socket, &QTcpSocket::disconnected, m_socket, &QObject::deleteLater);
connect(m_socket, &QIODevice::readyRead, this, &ChkUpdate::readReady);
m_inStream.setDevice(m_socket);
m_inStream.setVersion(QDataStream::Qt_5_0);

}
void ChkUpdate::runUp()
{
m_inStream << "queryFiles";
m_socket->waitForBytesWritten(50000);
}
ChkUpdate::~ChkUpdate()
{
if (m_socket!=nullptr)
delete m_socket;
delete serverIP;
}
void ChkUpdate::readReady()
{
if (0 == m_fileSize && m_socket->bytesAvailable() > sizeof(qint64))
{
m_inStream >> m_fileSize >> m_fileName;
m_file.setFileName("update\\"+m_fileName);
if (!m_file.open(QIODevice::WriteOnly)) {
qCritical() << m_file.errorString();
return;
}
}
qint64 size = qMin(m_socket->bytesAvailable(), m_fileSize - m_fileByteReceived);
QByteArray arry(size, 0);
// qDebug() << "size=" << size;
m_inStream.readRawData(arry.data(), size);
m_file.write(arry);
m_fileByteReceived += size;
if (m_fileByteReceived == m_fileSize) {
m_file.close();
reset();
}
}


void ChkUpdate::reset()
{

m_fileName.clear();
m_fileSize = 0;
m_fileByteReceived = 0;
}

void ChkUpdate::onSocketError(QAbstractSocket::SocketError err)
{
switch (err) {
case QAbstractSocket::ConnectionRefusedError:
qDebug() << "QAbstractSocket::ConnectionRefusedError";
break;
case QAbstractSocket::RemoteHostClosedError:
qDebug() << "QAbstractSocket::RemoteHostClosedError";
// reset();
break;
case QAbstractSocket::HostNotFoundError:
qDebug() << "QAbstractSocket::HostNotFoundError";
break;
case QAbstractSocket::SocketAccessError:
qDebug() << "QAbstractSocket::SocketAccessError";
break;
case QAbstractSocket::SocketResourceError:
qDebug() << "QAbstractSocket::SocketResourceError";
break;
case QAbstractSocket::SocketTimeoutError:
qDebug() << "QAbstractSocket::SocketTimeoutError";
break;
case QAbstractSocket::DatagramTooLargeError:
qDebug() << "QAbstractSocket::DatagramTooLargeError";
break;
case QAbstractSocket::NetworkError:
qDebug() << "QAbstractSocket::NetworkError";
break;
case QAbstractSocket::AddressInUseError:
qDebug() << "QAbstractSocket::AddressInUseError";
break;
case QAbstractSocket::SocketAddressNotAvailableError:
qDebug() << "QAbstractSocket::SocketAddressNotAvailableError";
break;
case QAbstractSocket::UnsupportedSocketOperationError:
qDebug() << "QAbstractSocket::UnsupportedSocketOperationError";
break;
case QAbstractSocket::ProxyAuthenticationRequiredError:
qDebug() << "QAbstractSocket::ProxyAuthenticationRequiredError";
break;
case QAbstractSocket::SslHandshakeFailedError:
qDebug() << "QAbstractSocket::SslHandshakeFailedError";
break;
case QAbstractSocket::UnfinishedSocketOperationError:
qDebug() << "QAbstractSocket::UnfinishedSocketOperationError";
break;
case QAbstractSocket::ProxyConnectionRefusedError:
qDebug() << "QAbstractSocket::ProxyConnectionRefusedError";
break;
case QAbstractSocket::ProxyConnectionClosedError:
qDebug() << "QAbstractSocket::ProxyConnectionClosedError";
break;
case QAbstractSocket::ProxyConnectionTimeoutError:
qDebug() << "QAbstractSocket::ProxyConnectionTimeoutError";
break;
case QAbstractSocket::ProxyNotFoundError:
qDebug() << "QAbstractSocket::ProxyNotFoundError";
break;
case QAbstractSocket::ProxyProtocolError:
qDebug() << "QAbstractSocket::ProxyProtocolError";
break;
case QAbstractSocket::OperationError:
qDebug() << "QAbstractSocket::OperationError";
break;
case QAbstractSocket::SslInternalError:
qDebug() << "QAbstractSocket::SslInternalError";
break;
case QAbstractSocket::SslInvalidUserDataError:
qDebug() << "QAbstractSocket::SslInvalidUserDataError";
break;
case QAbstractSocket::TemporaryError:
qDebug() << "QAbstractSocket::TemporaryError";
break;
case QAbstractSocket::UnknownSocketError:
qDebug() << "QAbstractSocket::UnknownSocketError";
break;
default:
break;
}

}

标签:case,QTcpServer,socket,更新,break,QTcpSoket,QAbstractSocket,qDebug,UpdateServer
来源: https://www.cnblogs.com/tonny1000/p/14396606.html