该程序能够发送 .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();
}