willchen
初階會員
發表:64 回覆:81 積分:29 註冊:2003-05-23
發送簡訊給我
|
以下是我寫的程式 Client部分
=====================================================================
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
char buf[512];
TFileStream *Client;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Tcp_Client->Host="140.116.39.126";
Tcp_Client->Port=1111;
//GetCurrentDirectory(512,buf);
//AnsiString dir=buf;
//Client=new TFileStream(dir "\\Client_Stream.txt",fmOpenRead);
//Edit1->Text=Client->Size;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Tcp_Client->Connect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
if(FileExists(OpenDialog1->FileName))
{
Client=new TFileStream(OpenDialog1->FileName,fmOpenRead);
Edit1->Text=Client->Size;
Tcp_Client->WriteLn(OpenDialog1->FileName);
Tcp_Client->WriteStream(Client,True,True);
delete Client;
}
}
}
//---------------------------------------------------------------------------
=====================================================================
Server部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString dir;
TFileStream *receive;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Server->DefaultPort=1111;
char buf[512];
GetCurrentDirectory(512,buf);
dir=buf; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Server->Active=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerExecute(TIdPeerThread *AThread)
{
AnsiString Filename=AThread->Connection->ReadLn();
receive=new TFileStream(dir Filename,fmCreate);
AThread->Connection->ReadStream(receive,-1,False);
delete receive;
}
//---------------------------------------------------------------------------
===================================================================== 基本上我是用兩個method,Client端利用WriteLn與WriteStream來傳資料,
WriteLn來接收Client端讀檔的檔名,WriteStream來接收Client端的資料.
請問各位高手,我在還沒寫檔名的功能前(也就是還沒把檔案名稱傳過來給Server,未使用WriteLn method時,本身傳檔案沒問題),是不是在INDY元件內,一個thread Execute只能給予一個執行method??還是我本身程式的寫法有錯誤之處??
可以給我一點提示嗎??謝謝,還是有其他更好的方法來傳檔案的名稱??
|
brook
資深會員
發表:57 回覆:323 積分:371 註冊:2002-07-12
發送簡訊給我
|
我覺得可將檔名加在 TMemoryStream 的最尾端的13個char會比較好處理,如此你的接收端就知道檔名就是最後的13個char,檔案大小就是原來的大小減13.
|
RaynorPao
版主
發表:139 回覆:3622 積分:7025 註冊:2002-08-12
發送簡訊給我
|
引言:
以下是我寫的程式 Client部分
=====================================================================
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
char buf[512];
TFileStream *Client;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Tcp_Client->Host="140.116.39.126";
Tcp_Client->Port=1111;
//GetCurrentDirectory(512,buf);
//AnsiString dir=buf;
//Client=new TFileStream(dir "\\Client_Stream.txt",fmOpenRead);
//Edit1->Text=Client->Size;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Tcp_Client->Connect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
if(FileExists(OpenDialog1->FileName))
{
Client=new TFileStream(OpenDialog1->FileName,fmOpenRead);
Edit1->Text=Client->Size;
Tcp_Client->WriteLn(OpenDialog1->FileName);
Tcp_Client->WriteStream(Client,True,True);
delete Client;
}
}
}
//---------------------------------------------------------------------------
=====================================================================
Server部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString dir;
TFileStream *receive;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Server->DefaultPort=1111;
char buf[512];
GetCurrentDirectory(512,buf);
dir=buf; }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Server->Active=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerExecute(TIdPeerThread *AThread)
{
AnsiString Filename=AThread->Connection->ReadLn();
receive=new TFileStream(dir Filename,fmCreate);
AThread->Connection->ReadStream(receive,-1,False);
delete receive;
}
//---------------------------------------------------------------------------
===================================================================== 基本上我是用兩個method,Client端利用WriteLn與WriteStream來傳資料,
WriteLn來接收Client端讀檔的檔名,WriteStream來接收Client端的資料.
請問各位高手,我在還沒寫檔名的功能前(也就是還沒把檔案名稱傳過來給Server,未使用WriteLn method時,本身傳檔案沒問題),是不是在INDY元件內,一個thread Execute只能給予一個執行method??還是我本身程式的寫法有錯誤之處??
可以給我一點提示嗎??謝謝,還是有其他更好的方法來傳檔案的名稱??
willchen 你好:
試試看這樣子寫可不可以呢??
< class="code">
// Client
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
IdTCPClient1->Host="192.168.16.152";
IdTCPClient1->Port=4000;
IdTCPClient1->Connect(3000);
}
__fastcall TForm1::~TForm1()
{
IdTCPClient1->Disconnect();
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(IdTCPClient1->Connected())
{
TFileStream *fs=new TFileStream("24bit.bmp", fmOpenRead);
try
{
IdTCPClient1->WriteLn("24bit.bmp");
IdTCPClient1->WriteStream(fs, true, true, 0);
}
__finally
{
delete fs;
}
}
}
// Server
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
IdTCPServer1->DefaultPort=4000;
IdTCPServer1->Active=true;
}
__fastcall TForm1::~TForm1()
{
IdTCPServer1->Active=false;
}
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
AnsiString szFileName=AThread->Connection->ReadLn();
TFileStream *fs=new TFileStream(szFileName, fmCreate);
try
{
AThread->Connection->ReadStream(fs, -1, false);
}
__finally
{
delete fs;
}
}
-- Enjoy Researching & Developing --
------ -- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
|
willchen
初階會員
發表:64 回覆:81 積分:29 註冊:2003-05-23
發送簡訊給我
|
謝謝包子哥的建議,我會試試看能不能Work...多謝指教
|