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();
    }
}

功能已測試可用,實現效果如下:
這裏寫圖片描述

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