首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

Help!运用bcb中fastnet组件中的NMUDP怎么进行网络数据传输?

2012-03-23 
Help!!!运用bcb中fastnet组件中的NMUDP如何进行网络数据传输??如题!文字说明其过程或编程实例都需要!!提供

Help!!!运用bcb中fastnet组件中的NMUDP如何进行网络数据传输??
如题!文字说明其过程或编程实例都需要!!提供查询网站或好用的书籍也ok!!实在是找不到啊!!谢谢啦!!

[解决办法]
//---------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "SRData.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Memo1->Clear();
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
Edit3->Text=OpenDialog1->FileName;
}
Memo1->Text="";
}
//---------------------------------------
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];
//char Buffer[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);
//BuffSize-=ThreshHold;
}
else
{
Strm->Read(Buffer,BuffSize);
NMUDP1->SendBuffer(Buffer,ThreshHold,ThreshHold);
}
BuffSize-=ThreshHold;
// memset(&Buffer,0,sizeof(Buffer));
ShowMessage("hehe");
}
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;
i=0;
//读取接收到的文件流
try{
NMUDP1->ReadBuffer(TmpBuffer,NumberBytes,i);
TmpBuffer[NumberBytes]='\0';
Memo1->Text=Memo1->Text+TmpBuffer;
}
__finally{
delete[] TmpBuffer;
}
}
//---------------------------------------


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

热点排行