#include "SendfileThread.h"#include "GBK.h"#include <QMessageBox>#include "osapi/osapi.h"SendfileThread::SendfileThread(QString name,QString mylocal,QString mypeer,QObject *parent): QThread(parent),flag(0){filename=name;allCount=100;readCount=20;localIp=mylocal;peerIp=mypeer;}SendfileThread::~SendfileThread(){}void SendfileThread::run(){FILE* file=fopen(GBK::FromUnicode(filename).c_str(),"rb");if (!file){QMessageBox::information(NULL,"ERROR",GBK::ToUnicode("无法打开该文件"));return;}fseek(file,0,SEEK_END);allCount=ftell(file);fseek(file,0,SEEK_SET);int part=0;int index=0;//创建一个网络通道OS_SockAddr local(GBK::FromUnicode(localIp).c_str(),9002);OS_UdpSocket sock;sock.Open(local,true);OS_SockAddr peer(GBK::FromUnicode(peerIp).c_str(),9003);while(1){if (index==0){string str;str=GBK::FromUnicode(filename);sock.SendTo(str.c_str(),218,peer);index=1;}else{char buf[218];int n=fread(buf,1,218,file);if (n<=0){char buf3[218]="end";sock.SendTo(buf3,218,peer);break;}if (n==218){sock.SendTo(buf,218,peer);}if (n>0&&n<128){buf[n]=0;sock.SendTo(buf,218,peer);}readCount+=n;// 每隔一定时间, sleep一次part += n;if(part > 32){QThread::msleep(500); // 模拟一下part = 0;}QThread::msleep(50);}}fclose(file);flag=1;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。