当前位置: 技术问答>linux和unix
学过Qt的前辈帮个忙
来源: 互联网 发布时间:2015-11-29
本文导语: 我在用Qt做文件传输的时候发生了一个情况 当我发送一个文本文件的时候,在接收方接到的是文本文件的一部分,这一部分重复接收 求高手给点建议 ============================================================================ 发送端 ...
我在用Qt做文件传输的时候发生了一个情况
当我发送一个文本文件的时候,在接收方接到的是文本文件的一部分,这一部分重复接收
求高手给点建议
============================================================================
发送端
void Sender::ConnectTo() //与接收端取得连接
{
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost(Ipedit->text(),5002);
connect(tcpSocket, SIGNAL(connected ()), this, SLOT(SendFile()));
}
void Sender::SendFile()//向接收端发送文件
{
int bytesWrite = 0;
fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
QFile Sendfile(fileName);
Sendfile.open(QIODevice::ReadOnly);
QDataStream stream( tcpSocket );
QFileInfo f(fileName);
fileName = f.fileName();
int filesize =f.size();
stream > fileName;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Append))
{
QMessageBox::warning(this,"error","can not open the file!");
return;
}
while (1)
{
if( clientConnection->isReadable())
{
if(readsize + 2048 read(buf, 2048);
file.write(buf, 2048);
readsize += 2048;
delete []buf;
if(readsize == filesize) break;
}
else
{
int blocksize = filesize - readsize;
buf = new char[blocksize];
clientConnection->read(buf, blocksize);
file.write(buf, blocksize);
readsize += blocksize;
delete []buf;
if(readsize == filesize) break;
}
}
}
file.close();
clientConnection->disconnect();
}
当我发送一个文本文件的时候,在接收方接到的是文本文件的一部分,这一部分重复接收
求高手给点建议
============================================================================
发送端
void Sender::ConnectTo() //与接收端取得连接
{
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost(Ipedit->text(),5002);
connect(tcpSocket, SIGNAL(connected ()), this, SLOT(SendFile()));
}
void Sender::SendFile()//向接收端发送文件
{
int bytesWrite = 0;
fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
QFile Sendfile(fileName);
Sendfile.open(QIODevice::ReadOnly);
QDataStream stream( tcpSocket );
QFileInfo f(fileName);
fileName = f.fileName();
int filesize =f.size();
stream > fileName;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Append))
{
QMessageBox::warning(this,"error","can not open the file!");
return;
}
while (1)
{
if( clientConnection->isReadable())
{
if(readsize + 2048 read(buf, 2048);
file.write(buf, 2048);
readsize += 2048;
delete []buf;
if(readsize == filesize) break;
}
else
{
int blocksize = filesize - readsize;
buf = new char[blocksize];
clientConnection->read(buf, blocksize);
file.write(buf, blocksize);
readsize += blocksize;
delete []buf;
if(readsize == filesize) break;
}
}
}
file.close();
clientConnection->disconnect();
}
|
建议一个比较WC的方法,声明两个成员:
bool m_firstReceive;
slot:
void readyReceive();
下面是对这个成员函数的定义:
void Sender::readyReceive()
{
if(!m_firstReceive)
{
m_firstReceive = True;
ReceiveFile();
}
}
注意,m_firstReceive的赋值,初始为False,然后更改你上面的connect:
connect(clientConnection, SIGNAL(readyRead()),this,SLOT(readyReceive()));
bool m_firstReceive;
slot:
void readyReceive();
下面是对这个成员函数的定义:
void Sender::readyReceive()
{
if(!m_firstReceive)
{
m_firstReceive = True;
ReceiveFile();
}
}
注意,m_firstReceive的赋值,初始为False,然后更改你上面的connect:
connect(clientConnection, SIGNAL(readyRead()),this,SLOT(readyReceive()));
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。