webkiller 发表于 2011-12-26 15:47:55

如何使用组件在主机间传送文件

Mainform.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

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

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
    this->OpenDialog1->Filter="Text files(*.txt)|*.txt|All files(*.*)|*.*";
    if(this->OpenDialog1->Execute())
    {
      this->Edit1->Text=this->OpenDialog1->FileName;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
if(this->Edit1->Text.Length()<1)
    return;
if(this->Edit2->Text.Length()<1)
    return;
TFileStream *MyFStream;
MyFStream=new TFileStream(this->Edit1->Text,fmOpenRead);
try
{
    this->NMStrm1->Host=this->Edit2->Text;
    this->NMStrm1->FromName="NetMasters";
    this->NMStrm1->PostIt(MyFStream);
    MessageBox(Handle,"发送文件到目标主机操作成功!","信息提示",MB_OK);
}
catch(...)
{
    MessageBox(Handle,"发送文件到目标主机操作失败!","信息提示",MB_OK);
}
MyFStream->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
this->Close();      
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrmServOnMSG(TComponent *Sender,
      const AnsiString sFrom, TStream *strm)
{
if(this->SaveDialog1->Execute())
{
    if(FileExists(this->SaveDialog1->FileName))
    {
      if(MessageBox(Handle,"目标文件已经存在,是否删除之?","信息提示",MB_YESNO)==IDNO)
      {
      return;
      }
   DeleteFile(this->SaveDialog1->FileName);
    }
    TFileStream *MyFStream;
    //建立文件
    MyFStream=new TFileStream(this->SaveDialog1->FileName,fmCreate);
    try
    {
       MyFStream->CopyFrom(strm,strm->Size);
       MessageBox(Handle,"接收目标主机发来的文件操作成功!","信息提示",MB_OK);
    }
    catch(...)
    {
       MessageBox(Handle,"接收目标主机发来的文件操作失败!","信息提示",MB_OK);
    }
    MyFStream->Free();
}
}
//---------------------------------------------------------------------------



Mainform.h

//---------------------------------------------------------------------------

#ifndef MainformH
#define MainformH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <Dialogs.hpp>
#include <NMSTRM.hpp>
#include <Psock.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
      TLabel *Label1;
      TEdit *Edit1;
      TLabel *Label2;
      TEdit *Edit2;
      TBitBtn *BitBtn1;
      TBitBtn *BitBtn2;
      TBitBtn *BitBtn3;
      TOpenDialog *OpenDialog1;
      TSaveDialog *SaveDialog1;
      TNMStrm *NMStrm1;
      TNMStrmServ *NMStrmServ1;
      void __fastcall BitBtn1Click(TObject *Sender);
      void __fastcall BitBtn2Click(TObject *Sender);
      void __fastcall BitBtn3Click(TObject *Sender);
      void __fastcall NMStrmServOnMSG(TComponent *Sender,
          const AnsiString sFrom, TStream *strm);
private:    // User declarations
public:      // User declarations
      __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


Transfer.cpp


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Mainform.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
      try
      {
               Application->Initialize();
               Application->CreateForm(__classid(TForm1), &Form1);
               Application->Run();
      }
      catch (Exception &exception)
      {
               Application->ShowException(&exception);
      }
      catch (...)
      {
               try
               {
                         throw Exception("");
               }
               catch (Exception &exception)
               {
                         Application->ShowException(&exception);
               }
      }
      return 0;
}
//---------------------------------------------------------------------------


页: [1]
查看完整版本: 如何使用组件在主机间传送文件