双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

udp的小程序,发送方如何最简单的方法以同文件名强行保存到接收方的任意路径下呢。

cnjsnt1995 cnjsnt1995 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//Unit1.cpp---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
Edit3->Text=OpenDialog1->FileName;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Edit3->Text=="")
{
ShowMessage("请选择一个您要传送的文件.");
return;
}
NMUDP1->RemoteHost=Edit1->Text;
NMUDP1->RemotePort=StrToInt(Edit2->Text);
NMUDP1->ReportLevel=Nmudp::Status_Basic;
const int ThreshHold=2048;
//载入文件流
TFileStream *Strm=new TFileStream(Edit3->Text, fmOpenRead);
char *Buffer=new char[ThreshHold];
int BuffSize=Strm->Size;
int Buffers=BuffSize/ThreshHold;
for(int i=0;i<=Buffers;i++)
{
if(BuffSize>2048)
{
Strm->Read(Buffer,ThreshHold);
NMUDP1->SendBuffer(Buffer,ThreshHold,ThreshHold);
}
else
{
Strm->Read(Buffer,BuffSize);
NMUDP1->SendBuffer(Buffer,BuffSize,BuffSize);
}
BuffSize-=ThreshHold;
}
delete Buffer;
delete Strm;
Memo1->Lines->Add("");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
char *TmpBuffer=new char[NumberBytes+1];
int i;
//读取接收到的文件流
NMUDP1->ReadBuffer(TmpBuffer,NumberBytes,i);
TmpBuffer[NumberBytes]='\0';
Memo1->Text=Memo1->Text+TmpBuffer;
delete[] TmpBuffer;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NMUDP1DataSend(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="文件传送完毕.";
}
//---------------------------------------------------------------------------
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部