18,356
社区成员
发帖
与我相关
我的任务
分享
if (m_client->bytesAvailable() > 0)
{
QByteArray buffer = m_client->read(MAX_BUFFER_SIZE));
//这里的这个buffer总是8192,为什么?
}
int FileSender::sendFile(QString strFilePath, QString strFileName)
{
//打开文件
QFile file(strFilePath);
if (file.open(QIODevice::ReadOnly) == false)
{
return -1;
}
//分配缓冲区
struct file_head *head = (struct file_head*)malloc(MAX_BUFFER_SIZE);
if (head == NULL)
{
return -1;
}
//获得缓冲区指针
char *pdata = head->data;
int datalen = MAX_BUFFER_SIZE - sizeof(struct file_head);
//设置数据头中不变的部分
memset(head, 0, sizeof(struct file_head));
strcpy(head->filename, strFileName.toLatin1().data());
head->totallen = file.size();
//获取数据
unsigned int len = 0, offset = 0;
while ((len = file.read(pdata, datalen)) > 0)
{
head->offset = offset;
head->datalen = len;
offset += len;
write((char*)head, sizeof(struct file_head) + len);
flush();
waitForBytesWritten();
//通知进度
emit writeChanged(strFilePath, ((double)(head->offset + head->datalen) / head->totallen) * 100);
}
file.close();
free(head);
return 0;
}
if (m_client->bytesAvailable() > 8192)
{
QByteArray buffer = m_client->read(MAX_BUFFER_SIZE));
//......
}