使用QUdpSocket和QDatastream发送UDP数据报

时间:2016-05-03 11:01:16

标签: c++ qt qfile qdatastream qudpsocket

该程序能够发送.txt文件。但是任何其他文件类型都会被破坏。我认为这与我存储框架的数据类型有关。抱歉,对于长篇文章我不知道我的问题可能在哪里。

我是QT和C ++的新手。我必须使用QUdpSocket通过网络将任何文件发送到另一台计算机。然后,此计算机应以正确的顺序重新组合数据报并将其另存为文件。 (QFiles必须按序列号标题分段,并根据这些数字重新组合)。段与它们的序列号一起存储在QByteArray数组中。

//Setup File
filelocation= opendialog->getOpenFileName(this, "Select File to be sent", "C:/Users/<USER>/Desktop");
SentFile.setFileName(filelocation);
if (!(SentFile.open(QIODevice::ReadOnly))){
    return;
}

//Setup Datastream
SentData.setDevice(&SentFile);
SentData.setByteOrder(QDataStream::BigEndian);
SentData.setFloatingPointPrecision(QDataStream::SinglePrecision);
SentData.setVersion(QDataStream::Qt_5_5);

//Calculate number of frames to be sent
max_framesize=200;
int length=SentFile.size();
dsize=length/max_framesize+1;
if((length%max_framesize)!=0)
    dsize++;

//Write initial message with dsize and file name
char BeginMessage[max_framesize+16];
char *cfile= new char[filelocation.length()+1];
strcpy(cfile, filelocation.toStdString().c_str());

sprintf(BeginMessage,"%016d%010d%s",0, dsize-1, cfile);
SendArray[0]= BeginMessage;

int i=1;
while(SentData.atEnd()!=true)
    {

    char s[max_framesize+1];

    int len = SentData.readRawData(s, max_framesize);
    for(int n=len; n<=max_framesize;n++)
    {
        s[n]='\0';
    }
    char tempframe[17];
    sprintf(tempframe,"%016d",i);
    tempframe[16]='\0';

    SendArray[i]=s;
    SendArray[i].prepend(tempframe);
    i++;
    }

然后通过qudpsocket

发送QByteArray数组
void MainWindow::on_pushButton_clicked()
{

for(int l=0; l<dsize; l++)
    {
    QByteArray datagram = SendArray[l];
    socket->writeDatagram(datagram.data(), QHostAddress::Broadcast, 5743);
    }
    ui->lineEdit->clear();
    ui->lineEdit->setFocus();
 }

重组代码的工作原理如下

void MainWindow::receiveData()
{
while (socket->hasPendingDatagrams())
{
  FramesReceived++;

    QByteArray datagram;

    datagram.resize(socket->pendingDatagramSize());
    QHostAddress sender;
    quint16 senderPort;
    socket->readDatagram(datagram.data(), datagram.size(),
                            &sender, &senderPort);

    char temp[datagram.size()];

    strcpy(temp, datagram);

     char cseq_num[17];
         for(int n=0; n<16;n++)
         {
           cseq_num[n]=temp[n];
         }
         cseq_num[16]='\0';
         datagram.remove(0,16);

     int seq_num = atoi(cseq_num);
     if(seq_num==0)
     {
         char size[11];
             for(int n=0; n<10;n++)
             {
               size[n]=temp[n+16];
             }
             size[11]='\0';
          rsize = atoi(size);

        QByteArray   Name=datagram;
         Name.remove(0, 10);

         strcpy(OriginalName,Name.data());
     }
    ReceivedArray[seq_num]= datagram;
    char message[600];

   sprintf(message,"%d of %d Frames Received\nFile name: %s",FramesReceived,rsize, OriginalName);
ui->label->setText(message);
}
}

收到所有帧后

void MainWindow::on_pushButton_3_clicked()
{

filelocation= opendialog->getSaveFileName(this, "Save File");
ReceivedFile.setFileName(filelocation);
if (!(ReceivedFile.open(QIODevice::WriteOnly))){
    return;
}

char snum[27];

    for(int n=0; n<27;n++)
    {
      snum[n]=ReceivedArray[0][n];
    }
    snum[27]='\0';

int len_num = atoi(snum);


ReceivedData.setDevice(&ReceivedFile);
ReceivedData.setByteOrder(QDataStream::BigEndian);
ReceivedData.setFloatingPointPrecision(QDataStream::SinglePrecision);
ReceivedData.setVersion(QDataStream::Qt_5_5);
for(int i=1; i<len_num; i++)
{
ReceivedData.writeRawData(ReceivedArray[i],ReceivedArray[i].length());
}

ReceivedData.writeRawData(ReceivedArray[len_num],ReceivedArray[len_num].length());
ReceivedFile.close();
}

0 个答案:

没有答案