willchen
初階會員
發表:64 回覆:81 積分:29 註冊:2003-05-23
發送簡訊給我
|
我最近try INDY元件使用它的兩個元件來作,並使用WriteStream與ReadStream
的method來傳輸Stream資料
但我發現Test過後,傳輸上沒問題.但在存檔時發現存檔後面有多了一些不該出現的文字(txt檔)
請問這是什麼原因??
以下是我的程式內容
Server部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TFileStream *receive;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Server->DefaultPort=1111;
receive=new TFileStream("I:\\Client-Server Stream檔案測試\\Server\\ServerFile.txt",fmCreate);
}
//------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Server->Active=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerExecute(TIdPeerThread *AThread)
{
int bytes=AThread->Connection->ReadInteger(true);
AThread->Connection->ReadStream(receive,bytes,False);
receive->Write(receive,bytes);
}
//------------------------------------------------------------------- Client端部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
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;
Client=new TFileStream("I:\\Client-Server Stream檔案測試\\Client\\Client_Stream.txt",fmOpenRead);
Edit1->Text=Client->Size;
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Tcp_Client->Connect();
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Tcp_Client->WriteStream(Client,True,False);
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Tcp_Client->WriteInteger(Client->Size,True);
}
//-------------------------------------------------------------------
|
RaynorPao
版主
發表:139 回覆:3622 積分:7025 註冊:2002-08-12
發送簡訊給我
|
引言:
我最近try INDY元件使用它的兩個元件來作,並使用WriteStream與ReadStream
的method來傳輸Stream資料
但我發現Test過後,傳輸上沒問題.但在存檔時發現存檔後面有多了一些不該出現的文字(txt檔)
請問這是什麼原因??
以下是我的程式內容
Server部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TFileStream *receive;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Server->DefaultPort=1111;
receive=new TFileStream("I:\\Client-Server Stream檔案測試\\Server\\ServerFile.txt",fmCreate);
}
//------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Server->Active=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerExecute(TIdPeerThread *AThread)
{
int bytes=AThread->Connection->ReadInteger(true);
AThread->Connection->ReadStream(receive,bytes,False);
// receive->Write(receive,bytes);
}
//------------------------------------------------------------------- Client端部分
===================================================================== #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
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;
Client=new TFileStream("I:\\Client-Server Stream檔案測試\\Client\\Client_Stream.txt",fmOpenRead);
Edit1->Text=Client->Size;
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Tcp_Client->Connect();
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Tcp_Client->WriteStream(Client,True,False);
Tcp_Client->WriteStream(Client, true, true, Client->Size);
}
//-------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// Tcp_Client->WriteInteger(Client->Size,True);
}
//-------------------------------------------------------------------
willchen 你好:
你的程式碼,我已經稍作修改 (以上標示為紅色的部分),並試過傳輸 5 MB 的
檔案,應該不會發生問題,不過我是使用 BCB 6.0 SP4 / Indy 9.00.11 元件
備註:
(1)BCB 6.0 原本附的是 Indy 8.0 元件
(2)如果想要把 BCB 6.0 的 Indy 元件昇級至 9.0 的話,可先在此下載 (包括
元件本身、說明文件、範例程式碼)
http://www.indyproject.org/download/Indy9.html
(3)下載完成以後,可依照以下連結文章中的步驟,安裝至 BCB
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=25182
-- Enjoy Researching & Developing --
------ -- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
|
willchen
初階會員
發表:64 回覆:81 積分:29 註冊:2003-05-23
發送簡訊給我
|
多謝包子哥的指教,小弟受益良多,會利用新版的INDY來做測試,剛剛試過安裝已經不成問題了,多謝,另外有一點要問包子哥的問題,也就是說用WriteStream來傳檔我之前有試過成功了,但是有個問題是,我要怎麼寫才能夠讓Server端知道我Client端所開啟要傳的檔名是什麼
因為我想要Client端傳到Server端時,Server端還是可以沿用Client端的檔名來做存檔,雖然有點離題,但我之前有po的那篇沒有人回,所以也只能沿用這個來繼續問下去,謝謝
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
willchen您好!
我想請教一下您用的元件名稱是那兩個(因為indy裡有太多元件),我也想試一下您的方法,先謝啦!
|
willchen
初階會員
發表:64 回覆:81 積分:29 註冊:2003-05-23
發送簡訊給我
|
在BCB 6.0中的元件列表的INDY Server 與INDY Client 你可以按照上次包子哥
的做法,把INDY元件更新到最新版,當然大部分的說明都是用Delphi寫的
希望你可以很快找到你要的東西
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
willchen謝謝您!我已經更新好Indy到最新版了… 我的動機其實是因為之前用bcb的ServerSocket及ClientSocket來傳送檔案,但是一直試不出來Client不回傳ack訊息而Server端能正常連續長期接收的方法,看到您所提到的TidTCPServer及TidTCPClient好像可以直接傳送檔案,而不用切封包或者回ack等,因此想試看看您的方式,初步試過了好像真的可以。
不過有一個問題,就是您的程式碼中好像是由Client傳給Server,正好與我要的在Client提出要求後由Server將檔案傳給Client相反,我目前還試不出來,不知道您或者是RaynorPao或者版上的大姐大哥們能否給個簡單的例子… 謝謝大家!
|