| 
 | 
 
 本帖最后由 海之梦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; 
........... 
} 
 |   
 
 
 
 |