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

cnjsnt1995 2006-06-01 09:40:00
//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="文件传送完毕.";
}
//---------------------------------------------------------------------------
...全文
306 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp616 2006-06-01
  • 打赏
  • 举报
回复
怎么保存是接收端的事。修改接收端啊。
yimawww 2006-06-01
  • 打赏
  • 举报
回复
强行保存?什么企图?
BlueDeepOcean 2006-06-01
  • 打赏
  • 举报
回复
能不能用SaveDialog呢?
cnjsnt1995 2006-06-01
  • 打赏
  • 举报
回复
发送方用一个Dialog,用于选择要发送的文件和用于选择接收端的保存的路径.接收方的文件名和发送方的文件名一样.
发送方是老师机,接收方是学生机.

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧