QT TCP Server

主要是实现了TCP服务器端的上位机应用,包括数据收发(字符串及十六进制数据),传输文件的功能,经测试可用。下面主要说明一下几个基于QT的函数:
服务器端读取数据函数:

void Widget::ServerReadData()
{
    QString s;
    QString buf;
    QByteArray data=clientConnection->readAll();
    message = QString(data);
if(message.contains ("clientStop")) 
{
       clientConnection->close ();
       ui->serverSendpushButton->setEnabled (false);
       ui->statuslabel->setText (tr("Client disconnect"));
       return;
    }

   if(this->FromHexEnable)
   {
      for(int i = 0; i < data.count(); i++)
      {
       s.sprintf("%02X ", data.at(i));
       buf += s;
      }
      ui->servertextBrowser->append(QString(buf));
   }
   else
      ui->servertextBrowser->append(QString(data));
}

服务器端发送数据函数:

void Widget::ServerSendData ()
{
    if(!clientConnection) 
        return;
    if(!(clientConnection = tcpServer->nextPendingConnection ())) //如果没有客户端连接
    {
        return;
    }
    QByteArray data;
    if (this->my16Enable)
    {
        data= QString2Hex(ui->serverMessagelineEdit->text());  //转换为16进制
    }else
        data.append(ui->serverMessagelineEdit->text());

    if(data.isEmpty ())
    {
        QMessageBox::warning (this, tr("Warnning"), tr("Please enter the send data"));
        return;
    }
    clientConnection->write(data);
}

获取IP地址函数:

void Widget::GetNetworkIP ()
{
    IPlist = QNetworkInterface::allAddresses ();

    foreach(QHostAddress IP, IPlist)
    {
        if(IP.protocol() == QAbstractSocket::IPv4Protocol)
        ui->serverIPcomboBox->addItem (IP.toString ());
    }

}

打开文件函数:

void Widget::openFile()
{
    fileName = QFileDialog::getOpenFileName(this);
    if(!fileName.isEmpty())
    {
        ui->readbin->setEnabled(true);
        ui->fileStatusLabel->setText(tr("Open file %1 success").arg(fileName));
    }
}

读取文件属性函数:

void Widget::startTransferfile()
{
    localFile = new QFile(fileName);
    if(!localFile->open(QFile::ReadWrite))
    {
        qDebug() << "open file error";
        return;
    }
    totalBytes = localFile->size();//文件总大小
    QString currentFileName = fileName.right(fileName.size()-fileName.lastIndexOf('/')-1);
    qDebug() << currentFileName;
    ui->fileStatusLabel->setText(tr("Sending..."));
    ui->progressBar->setMaximum(totalBytes);
    bytesToWrite = totalBytes;
    file_send_cnt = 0;
    timer->start(10);
}

发送文件并更新进度条函数:

void Widget::timerUpDate()
{
    if(file_send_cnt<=totalBytes)
    {
        timer->start(1000);
        //发送数据,并计算发送完数据后剩余数据的大小
        if(bytesToWrite <= 2048)
         bytesToWrite = bytesToWrite - clientConnection->write(localFile->read(bytesToWrite));
        else
        bytesToWrite = bytesToWrite - clientConnection->write(localFile->read(2048));
        //更新发送文件进度条
        ui->progressBar->setValue(totalBytes-bytesToWrite);
        file_send_cnt += 2048;
    }
    else
    {
        ui->fileStatusLabel->setText(tr("Send OK! "));
        localFile->close();
        tcpServer->close();
    }
}

功能已测试可用,实现效果如下:
这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章