QT文件传输(简单版)
QT文件传输(简单版)
这是用QT写的一篇简单的文件传输,只是考虑了黏包问题,并没有考虑进度条和线程问题
一、原理图
二、服务器端TCPSocket通信连接
Pro中加network
头文件:
QTcpSocket通信套接字
QTcpServer监听套接字
QFile 文件操作
QTimer定时器(防止黏包)
函数
- FileServer()//构造函数
FileServer()
{
ui->setupUi(this);
sendSize = 0;
setWindowTitle("服务器端口:8888");
m_tcpServer = new QTcpServer(this);
//隐藏按钮
ui->buttonSelect->setEnabled(false);
ui->buttonSend->setEnabled(false);
//监听
m_tcpServer->listen(QHostAddress::Any,8888);
connect(m_tcpServer,&QTcpServer::newConnection,
this, &FileServer::slotNewconnection);
//定时信号,当文件头信息发送完成时,发送文件本体
connect(&timer, &QTimer::timeout,
this, &FileServer::slotSend);
}
2.当有客户端连接时,打印客户端IP和端口号,及使选择文件按钮可点
void slotNewconnection();
{
m_tcpsocket = m_tcpServer->nextPendingConnection();
QString ip = m_tcpsocket->peerAddress().toString();
quint16 port = m_tcpsocket->peerPort();
QString str = QString("[%1 : %2]:成功连接").arg(ip).arg(port);
ui-