天嵌 ARM开发社区

 找回密码
 注册
查看: 1841|回复: 3

UDP发送图片

[复制链接]
海之梦fly 发表于 2013-5-3 10:44:46 | 显示全部楼层 |阅读模式
本帖最后由 海之梦fly 于 2013-5-3 11:34 编辑

用QT 写了个发送和接受图片,发现能正常显示的远小于不能显示的,不知哪里出问题
我每次用UDP发送128B,图片大小为30K,直到发送完成。接受端那里出现丢包现象,偶而会成功一次
请高人帮我看看,下面是代码:
发送端:
    connect(ui->sendButton,SIGNAL(clicked()),
            this,SLOT(sendDatagram()));
    connect(udpSocket,SIGNAL(bytesWritten(qint64)),            this,SLOT(UpdateProgress(qint64)));
void client::sendDatagram()
{    qDebug()<<"sending ......";    TotalByte=0;//   

havewritedByte=0;    leftByte=0;    outblock=0;   
loadSize=128;   //每次发送数据的大小    i
magefile=new QFile("./linux.png");   
if(!imagefile->open(QFile::ReadOnly))   
{        qDebug() << "open file error!";      
return;   
}    TotalByte=imagefile->size();
    //send datagram   

QDataStream out(&outblock,QIODevice::WriteOnly);//datastream   
out.setVersion(QDataStream::Qt_4_0);// set datastream setVersion   
outblock= imagefile->read(loadSize);//一次性读取128B数据   
udpSocket->writeDatagram(outblock,     
                 QHostAddress::Broadcast,//QHostAddress(ui->IPlineEdit->text()),         
                    ui->;PortlineEdit->text().toInt());}
void client::UpdateProgress(qint64 havesendbyte)
{    qDebug()<<"TotalByte is ......"<<TotalByte;   
havewritedByte+=havesendbyte;   
qDebug()<<"havewritedByte is ......"<<havewritedByte;   
leftByte=TotalByte-havewritedByte;   
qDebug()<<"leftByte is ......"<<leftByte;
   ui->sendProgressbar->setMaximum(TotalByte);   
ui->sendProgressbar->setValue(havewritedByte);  
  if(leftByte>0)   
{      
outblock= imagefile->read(qMin(leftByte,loadSize));      
  udpSocket->writeDatagram(outblock,                             
    QHostAddress::Broadcast,                        
       ui->;PortlineEdit->text().toInt());   
}   
else//发送完    {      
delete imagefile;        
udpSocket->close();      
outblock.resize(0);  
  }
}
接受端:   
//有收到数据就更新UI    connect( udpSocket, SIGNAL( readyRead() ),           
  this, SLOT( processDatagrms_slot() ) );
    connect( this, SIGNAL( haveDatagrams_signal( qint64, QByteArray ) ),      

      this, SLOT( updateServerProgress( qint64, QByteArray ) ) );
void server::processDatagrms_slot()
{   
  while ( udpSocket->hasPendingDatagrams() )   
{        this->inblock.resize( this->udpSocket->pendingDatagramSize() );      
//get 数据包大小      
  this->udpSocket->readDatagram( inblock.data(), inblock.size() );        
   
emit haveDatagrams_signal( this->inblock.size(), inblock);
    }

//接受over ,就显示图片   
QPixmap pixmap;//pixel map  
  pixmap.loadFromData( countblock ); //load  all image  byteArray data   
    if ( !pixmap.isNull() ) //is not image  
  {        qDebug()<<"ui->imagelabel_3->setPixmap( pixmap ) .....";     
   ui->imagelabel_3->setPixmap( pixmap ); //将图片加载到label      
ui->imagelabel_3->show();   
}      
  else     
   {   
      ui->imagelabel_3->setText("123...");      
  qDebug()<<"NO LOAD Image ..image is null...";     
   }
    inblock.resize(0);   

countblock.resize(0);  
  bytesReceived=0;  
}
//更新进度条
void server::updateServerProgress( qint64 byteshaveReceived, QByteArray havereceiveDatagrams )
{    qDebug() << "updateServerProgress  run ......";  
  countblock += havereceiveDatagrams;//累加数据包,用来显示图片  
bytesReceived += byteshaveReceived;   
qDebug() << "bytesReceived  is ......" << bytesReceived;
...........
}
Jangel 发表于 2013-5-3 10:52:31 | 显示全部楼层
求LZ处理好缩进,这样的代码是人看的么...
 楼主| 海之梦fly 发表于 2013-5-3 11:29:55 | 显示全部楼层
Jangel 发表于 2013-5-3 10:52
求LZ处理好缩进,这样的代码是人看的么...

不好意思,我发之前挺好看的,不知发上去会是这个结果,哈哈
 楼主| 海之梦fly 发表于 2013-5-3 11:34:52 | 显示全部楼层
本帖最后由 海之梦fly 于 2013-5-3 11:38 编辑
Jangel 发表于 2013-5-3 10:52
求LZ处理好缩进,这样的代码是人看的么...


用QT 写了个发送和接受图片,发现能正常显示的远小于不能显示的,不知哪里出问题
我每次用UDP发送128B,图片大小为30K,直到发送完成。接受端那里出现丢包现象,偶而会成功一次
请高人帮我看看,下面是代码:
发送端:
    connect(ui->sendButton,SIGNAL(clicked()),
            this,SLOT(sendDatagram()));
    connect(udpSocket,SIGNAL(bytesWritten(qint64)),         
       this,SLOT(UpdateProgress(qint64)));
void client::sendDatagram()
{   
qDebug()<<"sending ......";   
TotalByte=0;   
havewritedByte=0;   
leftByte=0;   
outblock=0;   
loadSize=128;   //每次发送数据的大小    i
magefile=new QFile("./linux.png");   
if(!imagefile->open(QFile::ReadOnly))   
{      
qDebug() << "open file error!";      
return;   
}   
TotalByte=imagefile->size();
    //send datagram   
QDataStream out(&outblock,QIODevice::WriteOnly);//datastream   
out.setVersion(QDataStream:t_4_0);// set datastream setVersion   
outblock= imagefile->read(loadSize);//一次性读取128B数据   
udpSocket->writeDatagram(outblock,  QHostAddress::Broadcast,        
                    ui->ortlineEdit->text().toInt());}
void client::UpdateProgress(qint64 havesendbyte)
{   
qDebug()<<"TotalByte is ......"<<TotalByte;   
havewritedByte+=havesendbyte;   
qDebug()<<"havewritedByte is ......"<<havewritedByte;   
leftByte=TotalByte-havewritedByte;   
qDebug()<<"leftByte is ......"<<leftByte;
ui->sendProgressbar->setMaximum(TotalByte);   
ui->sendProgressbar->setValue(havewritedByte);  
if(leftByte>0)   
{      
  outblock= imagefile->read(qMin(leftByte,loadSize));      
  udpSocket->writeDatagram(outblock,                             
  QHostAddress::Broadcast,                        
  ui->ortlineEdit->text().toInt());   
}   
else//发送完   
{      
    delete imagefile;        
    udpSocket->close();      
    outblock.resize(0);  
  }
}


接受端:   
//有收到数据就更新UI   
connect( udpSocket, SIGNAL( readyRead() ),           
         this, SLOT( processDatagrms_slot() ) );
connect( this, SIGNAL( haveDatagrams_signal( qint64, QByteArray ) ),      
              this, SLOT( updateServerProgress( qint64, QByteArray ) ) );
              
              
void server::processDatagrms_slot()
{   
while ( udpSocket->hasPendingDatagrams() )   
{        

        this->inblock.resize( this->udpSocket->pendingDatagramSize() );      
        //get 数据包大小      
         this->udpSocket->readDatagram( inblock.data(), inblock.size() );        
   
        emit haveDatagrams_signal( this->inblock.size(), inblock);
  }
//接受over ,就显示图片   
QPixmap pixmap;//pixel map  
pixmap.loadFromData( countblock ); //load  all image  byteArray data   
  if ( !pixmap.isNull() ) //is   image  
  {      
          qDebug()<<"ui->imagelabel_3->setPixmap( pixmap ) .....";     
          ui->imagelabel_3->setPixmap( pixmap ); //将图片加载到label      
        ui->imagelabel_3->show();   
}      
  else     
   {   
              ui->imagelabel_3->setText("123...");      
          qDebug()<<"NO LOAD Image ..image is null...";     
   }
inblock.resize(0);   
countblock.resize(0);  
  bytesReceived=0;  
}
//更新进度条
void server::updateServerProgress( qint64 byteshaveReceived, QByteArray havereceiveDatagrams )
{   
        qDebug() << "updateServerProgress  run ......";  
          countblock += havereceiveDatagrams;//累加数据包,用来显示图片  
        bytesReceived += byteshaveReceived;   
        qDebug() << "bytesReceived  is ......" << bytesReceived;
        ...........
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-29 04:40 , Processed in 1.025325 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表