⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendfile_cpp.cpp

📁 一个在delphi下用udp传输文件的实例
💻 CPP
📖 第 1 页 / 共 3 页
字号:
Sent->Enabled=false;
Add->Enabled=false;
Cancel->Enabled=true;
Clear->Enabled=false;
SetUp->Enabled=false;
FSRepa->Lines->Clear();
FSRepa->Lines->Add("CRC错误重发块");
FSCMDS->Lines->Clear();

}
//---------------------------------------------------------------------------


void __fastcall TSendFiles::StartSend()                 //开始发送文件
{
 FRCMDS->Lines->Clear();
 FRCMDS->Lines->Add(FRCMD); 
 if (FSTimeOut>MaxTimeOut->Text.ToIntDef(60)) return;      //超时
 AnsiString FileName=FullFileName->Strings[0];          //文件名
 try
    {
    delete ReadFile;
    ReadFile=NULL;
    ReadFile=new TFileStream(FileName,fmOpenRead|fmShareDenyNone);//打开文件
    }
 catch (...)
    {
    FSStatusBar->Panels->Items[1]->Text="打开文件失败!";
    }
 ReadFileSize=ReadFile->Size;                        //未读字节数
 AnsiString WriteName=ExtractFileName(FileName);     //文件名
 SendData_1("FSCrea",WriteName.c_str(),WriteName.Length());//发送建立文件命令
}

void __fastcall TSendFiles::FSTimerTimer(TObject *Sender) //发送方超时定时器计数器
{
FSTimeOut++;                                               //计数器
AnsiString CMD=LastSendData.FileFlage;                     //最后一次发送的命令
CMD=CMD.SubString(1,6);                                    //取出命令码
if (CMD!="FSName" && CMD!="FSFile")                        //如果发送的是文件名和文件块数据命令,不执行重发
  {
   int temp=FSTimeOut/3;                                   //3秒钟重发一次
   if (temp*3==FSTimeOut)
      {
       SendData_1("FSLast","",0);                          //重发
      }
  }
if (FSTimeOut<5) return;        //小于5秒不显示信息
if (FSTimeOut<20) FSStatusBar->Panels->Items[1]->Text=FSStatusBar->Panels->Items[1]->Text+".";
else FSStatusBar->Panels->Items[1]->Text="............."+IntToStr(FSTimeOut)+"秒";
if (FSTimeOut>MaxTimeOut->Text.ToIntDef(60))               //超时
   {
    FSTimer->Enabled=false;                               //关闭定时器
    if (CMD!="FSName") FSStatusBar->Panels->Items[1]->Text="发送失败,网络超时。";
    else FSStatusBar->Panels->Items[1]->Text="发送失败,对方无应答。";
    delete ReadFile;
    ReadFile=NULL;
    FSTimeOut=0;
    if (ListView1->Items->Count>0)
     {
      Sent->Enabled=true;                                //界面按钮设置
      Add->Enabled=false;
     }
    else
     {
      Sent->Enabled=false;
      Add->Enabled=true;
     }
    Clear->Enabled=true;
    SetUp->Enabled=true;
    Cancel->Enabled=false;
   }
}
//---------------------------------------------------------------------------



void __fastcall TSendFiles::CancelClick(TObject *Sender)     //发送过程中的"取消"
{
if (ReadFile!=NULL)
   {
    if (MessageDlg("正在发送文件...,停止吗!",mtConfirmation,TMsgDlgButtons() << mbYes<<mbNo,0)==mrNo) return;
    if (ListView1->Items->Count>0)                          //界面按钮设置
         {
          Sent->Enabled=true;
          Add->Enabled=false;
         }
    else
         {
          Sent->Enabled=false;
          Add->Enabled=true;
         }
    Clear->Enabled=true;
    SetUp->Enabled=true;
    Cancel->Enabled=false;
    AnsiString FileName=ExtractFileName(FullFileName->Strings[0]); //取出正在发送的文件名
    SendData_1("FSCanc",FileName.c_str(),FileName.Length());         //通知接收方中断发送
    try
       {
        delete ReadFile;                                             //关闭正在发送的文件
        ReadFile=NULL;
       }
    catch(...){}
    FSStatusBar->Panels->Items[1]->Text="用户中断";
    FSTimer->Enabled=false;
    FSTimeOut=0;
   }
if (WriteFile!=NULL)
   {
    if (MessageDlg("正在接收文件...,停止吗!",mtConfirmation,TMsgDlgButtons() << mbYes<<mbNo,0)==mrNo) return;
    delete WriteFile;
    WriteFile=NULL;
    Cancel->Enabled=false;
    SendData_1("FREnd","",0);
    SendData_1("FREnd","",0);
    SendData_1("FREnd","",0);
    SendData_1("FREnd","",0);
    FRStatusBar->Panels->Items[1]->Text="中断接收";
   }
}
//---------------------------------------------------------------------------


AnsiString __fastcall TSendFiles::GetDir()      //取保存文件的路径
{
 AnsiString Direct=Dir->Text;
 while (Direct.SubString(Direct.Length(),1)=="\\") Direct.Delete(Direct.Length(),1);
 return Direct;
}
//---------------------------------------------------------------------------


//**********生成CRC校验码表****************************
void __fastcall TSendFiles::BuildTable16(unsigned short aPoly)
{
    unsigned short i, j;
    unsigned short nData;
    unsigned short nAccum;

    for ( i = 0; i < 256; i++ )
    {
        nData = ( unsigned short )( i << 8 );
        nAccum = 0;
        for ( j = 0; j < 8; j++ )
        {
            if ( ( nData ^ nAccum ) & 0x8000 )
                nAccum = ( nAccum << 1 ) ^ aPoly;
            else
                nAccum <<= 1;
            nData <<= 1;
        }
        Table_CRC[i] = ( unsigned long )nAccum;
    }
}
//---------------------------------------------------------------------------


//**********计算CRC校验码****************************
unsigned short __fastcall TSendFiles::CRC_16( unsigned char * aData, unsigned long aSize )
{
    unsigned long  i;
    unsigned short nAccum = 0;

    for ( i = 0; i < aSize; i++ )
        nAccum = ( nAccum << 8 ) ^ ( unsigned short )Table_CRC[( nAccum >> 8 ) ^ *aData++];
    return nAccum;
}
//---------------------------------------------------------------------------


int __fastcall TSendFiles::SendFile_1(int Position)
{
 char Buffer[1024];
 int Size;
try
 {
 if (ReadFile==NULL) return 0;
 if (ReadFile->Size-Position>1000) Size=1000;
 else Size=ReadFile->Size-Position;
 ReadFile->Seek(Position,soFromBeginning);
 ReadFile->ReadBuffer(Buffer,Size);
 SendData_1("FSFile",Buffer,Size,Position);
 FSBlocks++;
 Delay(SleepTime);
 return Size;
 }
catch (...)
 {
 FSStatusBar->Panels->Items[1]->Text="读文件错误!";
 return -1;
 }
}
//---------------------------------------------------------------------------


void __fastcall TSendFiles::Timer1Timer(TObject *Sender)//文件传输计时器
{
float FSRate=FSBlocks*1000/1024.0;
float FRRate=FRBlocks*1000/1024.0;
FSBlocks=0;
FRBlocks=0;
TimerCount++;
int hh,mm,ss;
ss=TimerCount;
hh=ss/3600;
ss=ss-hh*3600;
mm=ss/60;
ss=ss-mm*60;
if (FRRate>0)
   {
    LastFRRate=(LastFRRate+FRRate)/2;
    FRStatusBar->Panels->Items[3]->Text=FormatFloat("0.0",LastFRRate)+"k/s";
    FRStatusBar->Panels->Items[2]->Text= FormatDateTime("hh:mm:ss",EncodeTime(hh,mm,ss,0));
    SendData_1("FRRate",(char *) &LastFRRate,sizeof(float));
   }
if (FSRate>0)
   {
    LastFSRate=(LastFSRate+FSRate)/2;
    if (Speed->Down)
       {
        float Rate0=(LastFSRate-Rate)/LastFSRate;
        if (Rate0<0) Rate0=-1*Rate0;
        if (Rate0>0 && Rate0>0.15) SleepTime+=1;
        if (Rate0>0 && SleepTime>1 && Rate0<0.05) SleepTime-=1;
       }
    else
       {
        if (LastFSRate>23) SleepTime+=10;
        if (LastFSRate<18) SleepTime-=10;
       }
    FSStatusBar->Panels->Items[3]->Text=FormatFloat("0.0",LastFSRate)+"k/s,"+IntToStr(SleepTime);
    FSStatusBar->Panels->Items[2]->Text= FormatDateTime("hh:mm:ss",EncodeTime(hh,mm,ss,0));
   }
}
//---------------------------------------------------------------------------

void __fastcall TSendFiles::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if (ReadFile!=NULL)
   {
    if (MessageDlg("正在发送文件...,停止吗!",mtConfirmation,TMsgDlgButtons() << mbYes<<mbNo,0)==mrYes)
       {
        delete ReadFile;
        ReadFile=NULL;
        CanClose=true;
       }
    else CanClose=false;
   }
if (WriteFile!=NULL)
   {
    if (MessageDlg("正在接收文件...,停止吗!",mtConfirmation,TMsgDlgButtons() << mbYes<<mbNo,0)==mrYes)
       {
        delete WriteFile;
        WriteFile=NULL;
        CanClose=true;
       }
    else CanClose=false;
   }
}
//---------------------------------------------------------------------------

void __fastcall TSendFiles::SpeedClick(TObject *Sender)
{
if (Speed->Down)
   {
    Speed->Caption="自动";
    Speed->Hint="自动调整到最佳速度";
   }
else
   {
    Speed->Caption="限速";
    Speed->Hint="限制传输速度以保证语音传送";
   }
}
//---------------------------------------------------------------------------



void __fastcall TSendFiles::Delay(int n)
{
long t1,t2;
t1=t2=timeGetTime();
while (abs(t2-t1)<n)
   {
    Application->ProcessMessages();
    t2=timeGetTime();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -