danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
大家好!又來叨擾大家了!
上次問大家的ServerSocket還有一點問題,就是每次若Client端中斷或者在連線之後,好像都無法把ServerSocket關閉(按視窗右角的X),每次都只能按Ctrl+Alt+Del好多次才能關閉,能否再請各位幫幫忙看到底為何會這樣子呢?是不是Timer寫得不好的關係?
再次感謝大家! //以下是小弟完整的Server程式
//---------------------------------------------------------------------------
#define MAX_STRLEN 16
#define MAX_FILENAME_LEN 255
#define MAX_DATASIZE 1024
typedef struct _MyPack
{
char szHeader[MAX_STRLEN];
char szFileName[MAX_FILENAME_LEN];
DWORD dwPackTotal;
DWORD dwPackCount;
DWORD dwTotalDataSize;
DWORD dwThisDataSize;
BYTE pbThisData[MAX_DATASIZE];
} MYPACK; #include
#pragma hdrstop #include "Unit1.h"
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
FILE *fp;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1->Enabled = false ;
JPEG_PATH = "c:\\My Documents\\My Pictures\\";
SLEEP_TIME = 50 ;
Timer1->Enabled = false ;
Timer1->Interval = 30000 ;
Timer2->Enabled = false ;
Timer2->Interval = 1000 ;
fp=fopen("c:\\server.msg","w");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ServerSocket1->Active = false ;
ServerSocket1->Close();
Timer1->Enabled = false ;
Timer2->Enabled = false ;
fclose(fp);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *msFile=new TMemoryStream;
OpenDialog1->Filter = "*.jpg";
if (OpenDialog1->Execute())
msFile->LoadFromFile(OpenDialog1->FileName);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iFileName.c_str());
mp.dwPackTotal=block;
mp.dwPackCount=i;
mp.dwTotalDataSize=msFile->Size;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); ServerSocket1->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
Sleep(SLEEP_TIME);
}
StatusBar1->SimpleText = OpenDialog1->FileName "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "接受" Socket->RemoteHost "之連線" ;
Button1->Enabled = true ;
Button1->Caption = "傳送圖片給" Socket->RemoteHost ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "與" Socket->RemoteHost "連線中" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "與" Socket->RemoteHost "連線中斷" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0 ;
StatusBar1->SimpleText = "Error from Client socket" ;
StatusBar1->SimpleText = "Client 端發生錯誤!已中斷與" Socket->RemoteHost
"之連線,請重新開啟伺服器" ;
for(int i=0; iSocket->ActiveConnections; i )
{
if(Socket->RemoteAddress==ServerSocket1->Socket->Connections[i]->RemoteAddress)
{
ServerSocket1->Socket->Disconnect(i);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = Socket->LocalHost " Port" Socket->LocalPort "等待用戶連線…" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::mnuListenClick(TObject *Sender)
{
ServerSocket1->Port=StrToInt(InputBox("伺服器設定","傳輸埠","4000"));
ServerSocket1->Open();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientMessage = Socket->ReceiveText();
AnsiString filename; if (ClientMessage==exp_ClientMessage)
{
ClientReceived = true ;
}
else if ((ClientMessage.Length()>=9))
{
if (ClientMessage=="Command_1")
{
for (int i=0;i<1000;i )
{
filename.printf("d",i&);
filename=JPEG_PATH filename ".jpg";
SendImage0(filename);
}
}
}
}
//---------------------------------------------------------------------------
void TForm1::SendImage0(AnsiString image_name)
{
TMemoryStream *msFile=new TMemoryStream;
msFile->LoadFromFile(image_name);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iSize;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); exp_ClientMessage = mp.szFileName ;
exp_ClientMessage = exp_ClientMessage IntToStr(mp.dwPackCount) IntToStr(mp.dwThisDataSize) ; ServerSocket1->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
ClientReceived = false ; LastPacketSize = mp.dwThisDataSize ; Timer1->Enabled = true ;
//在Server端未接獲Client端傳來訊息前暫時停止傳送封包
StatusBar1->SimpleText = "等待Client端回報訊息..." ;
Timer2->Enabled = true ;
elaps_time = 0;
while (!ClientReceived)
{
Application->ProcessMessages();
StatusBar1->SimpleText = "等待Client回應 :" IntToStr(elaps_time) "/" IntToStr(Timer1->Interval/1000) ;
}
Timer1->Enabled = false ;
Timer2->Enabled = false ;
}
StatusBar1->SimpleText = image_name "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
//void TForm1::SendImage(TObject *Sender,AnsiString image_name)
void TForm1::SendImage(TServerSocket *Sender,AnsiString image_name)
{
TMemoryStream *msFile=new TMemoryStream;
msFile->LoadFromFile(image_name);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iSize;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); Sender->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
}
StatusBar1->SimpleText = image_name "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SleepTime1Click(TObject *Sender)
{
SLEEP_TIME = StrToInt(InputBox("Sleep Time Setting","Sleep Time (ns) = ","100"));
}
//--------------------------------------------------------------------------- void __fastcall TForm1::jpegpath1Click(TObject *Sender)
{
JPEG_PATH = InputBox("JPEG Path Setting","The Path including JPEGs : ","c:\\My Documents\\My Pictures\\");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false ;
Timer2->Enabled = false ;
// ServerSocket1->Socket->Disconnect(0);
ServerSocket1->Active = false ;
// StatusBar1->SimpleText = ServerSocket1->Socket->Connections[0]->RemoteHost "沒有回應,連線中斷" ;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::WaitTime1Click(TObject *Sender)
{
Wait_Time = StrToInt(InputBox("Wait Time Setting : ","Wait Client for (ms) : ",""));
Timer1->Interval = Wait_Time ;
ServerSocket1->Socket->SendText("Clinet Response Timeout");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
elaps_time ;
}
//---------------------------------------------------------------------------
|
RaynorPao
版主
發表:139 回覆:3622 積分:7025 註冊:2002-08-12
發送簡訊給我
|
引言:
大家好!又來叨擾大家了!
上次問大家的ServerSocket還有一點問題,就是每次若Client端中斷或者在連線之後,好像都無法把ServerSocket關閉(按視窗右角的X),每次都只能按Ctrl Alt Del好多次才能關閉,能否再請各位幫幫忙看到底為何會這樣子呢?是不是Timer寫得不好的關係?
再次感謝大家! //以下是小弟完整的Server程式
//---------------------------------------------------------------------------
#define MAX_STRLEN 16
#define MAX_FILENAME_LEN 255
#define MAX_DATASIZE 1024
typedef struct _MyPack
{
char szHeader[MAX_STRLEN];
char szFileName[MAX_FILENAME_LEN];
DWORD dwPackTotal;
DWORD dwPackCount;
DWORD dwTotalDataSize;
DWORD dwThisDataSize;
BYTE pbThisData[MAX_DATASIZE];
} MYPACK; #include
#pragma hdrstop #include "Unit1.h"
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
FILE *fp;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1->Enabled = false ;
JPEG_PATH = "c:\\My Documents\\My Pictures\\";
SLEEP_TIME = 50 ;
Timer1->Enabled = false ;
Timer1->Interval = 30000 ;
Timer2->Enabled = false ;
Timer2->Interval = 1000 ;
fp=fopen("c:\\server.msg","w");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ServerSocket1->Active = false ;
ServerSocket1->Close();
Timer1->Enabled = false ;
Timer2->Enabled = false ;
fclose(fp);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *msFile=new TMemoryStream;
OpenDialog1->Filter = "*.jpg";
if (OpenDialog1->Execute())
msFile->LoadFromFile(OpenDialog1->FileName);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iFileName.c_str());
mp.dwPackTotal=block;
mp.dwPackCount=i;
mp.dwTotalDataSize=msFile->Size;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); ServerSocket1->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
Sleep(SLEEP_TIME);
}
StatusBar1->SimpleText = OpenDialog1->FileName "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "接受" Socket->RemoteHost "之連線" ;
Button1->Enabled = true ;
Button1->Caption = "傳送圖片給" Socket->RemoteHost ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "與" Socket->RemoteHost "連線中" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = "與" Socket->RemoteHost "連線中斷" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode = 0 ;
StatusBar1->SimpleText = "Error from Client socket" ;
StatusBar1->SimpleText = "Client 端發生錯誤!已中斷與" Socket->RemoteHost
"之連線,請重新開啟伺服器" ;
for(int i=0; iSocket->ActiveConnections; i )
{
if(Socket->RemoteAddress==ServerSocket1->Socket->Connections[i]->RemoteAddress)
{
ServerSocket1->Socket->Disconnect(i);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->SimpleText = Socket->LocalHost " Port" Socket->LocalPort "等待用戶連線…" ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::mnuListenClick(TObject *Sender)
{
ServerSocket1->Port=StrToInt(InputBox("伺服器設定","傳輸埠","4000"));
ServerSocket1->Open();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientMessage = Socket->ReceiveText();
AnsiString filename; if (ClientMessage==exp_ClientMessage)
{
ClientReceived = true ;
}
else if ((ClientMessage.Length()>=9))
{
if (ClientMessage=="Command_1")
{
for (int i=0;i<1000;i )
{
filename.printf("d",i&);
filename=JPEG_PATH filename ".jpg";
SendImage0(filename);
}
}
}
}
//---------------------------------------------------------------------------
void TForm1::SendImage0(AnsiString image_name)
{
TMemoryStream *msFile=new TMemoryStream;
msFile->LoadFromFile(image_name);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iSize;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); exp_ClientMessage = mp.szFileName ;
exp_ClientMessage = exp_ClientMessage IntToStr(mp.dwPackCount) IntToStr(mp.dwThisDataSize) ; ServerSocket1->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
ClientReceived = false ; LastPacketSize = mp.dwThisDataSize ; Timer1->Enabled = true ;
//在Server端未接獲Client端傳來訊息前暫時停止傳送封包
StatusBar1->SimpleText = "等待Client端回報訊息..." ;
Timer2->Enabled = true ;
elaps_time = 0;
while (!ClientReceived)
{
Application->ProcessMessages();
StatusBar1->SimpleText = "等待Client回應 :" IntToStr(elaps_time) "/" IntToStr(Timer1->Interval/1000) ;
}
Timer1->Enabled = false ;
Timer2->Enabled = false ;
}
StatusBar1->SimpleText = image_name "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
//void TForm1::SendImage(TObject *Sender,AnsiString image_name)
void TForm1::SendImage(TServerSocket *Sender,AnsiString image_name)
{
TMemoryStream *msFile=new TMemoryStream;
msFile->LoadFromFile(image_name);
MYPACK mp;
int i=0;
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(i=0; iSize;
mp.dwThisDataSize=((i 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-i*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=i*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize); Sender->Socket->Connections[0]->SendBuf(&mp, sizeof(MYPACK));
}
StatusBar1->SimpleText = image_name "(" mp.dwTotalDataSize "Byte)傳送完成" ;
delete msFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SleepTime1Click(TObject *Sender)
{
SLEEP_TIME = StrToInt(InputBox("Sleep Time Setting","Sleep Time (ns) = ","100"));
}
//--------------------------------------------------------------------------- void __fastcall TForm1::jpegpath1Click(TObject *Sender)
{
JPEG_PATH = InputBox("JPEG Path Setting","The Path including JPEGs : ","c:\\My Documents\\My Pictures\\");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false ;
Timer2->Enabled = false ;
// ServerSocket1->Socket->Disconnect(0);
ServerSocket1->Active = false ;
// StatusBar1->SimpleText = ServerSocket1->Socket->Connections[0]->RemoteHost "沒有回應,連線中斷" ;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::WaitTime1Click(TObject *Sender)
{
Wait_Time = StrToInt(InputBox("Wait Time Setting : ","Wait Client for (ms) : ",""));
Timer1->Interval = Wait_Time ;
ServerSocket1->Socket->SendText("Clinet Response Timeout");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
elaps_time ;
}
//---------------------------------------------------------------------------
danielj 你好:
(1)小弟我並沒有實際執行過你的程式碼 (因為沒有 client 端的程式)
(> --
------ -- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
|
Royce520
高階會員
發表:18 回覆:157 積分:100 註冊:2002-09-13
發送簡訊給我
|
danielj 你好,
看了程式的流程, 我的想法是這樣, 程式流程會是這樣: 我舉一個傳送案例
// 以下(c) 是 client 動作, (s) 是 server 動作
(c)client send "command_1" if Button1 of client is clicked.
(s)ServerSocket1ClientRead function would be executed. Then
it will enter SendImage0.
(s)In SendImage0, server would response a pack to client.
(c)ClientSocket1Read function would get a pack from server and
reply it.
(s)ServerSocket1ClientRead function would be called again.
===> 因此, 造成程式重進入的情形 *真實的事物最美, 簡單的道理最好, 我能體會的 太少*
------ 不要忘記呼吸,不要忘記編程! ∩__∩
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
多謝Royce520 大哥的回應!
不過小弟還是不太了解您的意思…
關於Button1Click的function基本上是不會進入的(當初只是做為初步測試用,現在已經改由Client端發送Command_1取代,因此此function應該可以不予理會)。 如果方便的話,能否再請您幫忙看一下小弟的程式那邊您覺得寫得不妥的,謝謝您!
|
Royce520
高階會員
發表:18 回覆:157 積分:100 註冊:2002-09-13
發送簡訊給我
|
danielj 你好,
由於你的程式是以非同步(nonblocking)方式來傳輸檔案,
所以並不會出現我之前我提的那個情形, 十分抱歉 造成
你的困擾 我實在是大錯特錯了
< class="code">
不過, 我碰到一個情形是 當client 程式的 button 第一次
按連線後 第二次再按會出現問題, 原因是在以下這行引起的
ClientSocket1->Socket->SendText("Command_1");
他會使得 server 進入 ServerSocket1ClientRead 的這個區塊
...
else if (ClientMessage.SubString(1,7) == "Command")
{
if (ClientMessage=="Command_1")
{
for (int i=0;i<1000;i )
{
filename.printf("d",i&);
filename=JPEG_PATH filename ".jpg";
SendImage0(filename);
}
}
...
因此, 我建議你改成這樣
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ClientSocket1->Active = false;
ClientSocket1->Address = "127.0.0.1";
ClientSocket1->Port = 4000 ;
ClientSocket1->Active = true ;
// ClientSocket1->Socket->SendText("Command_1");
StatusBar1->SimpleText = "Command_1 was sent" ;
} 第二, 在client的ClientSocket1Read中, 建議你修改一個地方
if (mp.dwThisDataSize < 1024)
Socket->SendText(ClientMessage);
因為, 非同步的方式 會使得在server 傳完檔案後才讓client將
資料傳過去, 所以到時 server讀到ClientMessage 將是像這種樣子
RecievedLength=1024RecievedLength=1024...RecievedLength=100
所以在下面的判斷式 是會出錯的 第三, 在server的Button1Click的函式中, 是不是要加上以下程式呢?
LastPacketSize = mp.dwThisDataSize;
因為ServerSocket1ClientRead會用到 不是嗎
我要謝謝你, 讓我有機會認識到 非同步傳輸 的一些問題 發表人 -
------ 不要忘記呼吸,不要忘記編程! ∩__∩
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
Royce520 多謝您的回答!
關於您寶貴的意見,第二點小弟愚笨,一直體會不出來,如果您方便的話可否再針對第二點做一些說明…
而關於第一點,小弟了解您的意思,如此就能讓Button1重覆被按下而不出錯…
而第三點的部分,可能是小弟的程式寫得太亂,小弟是嘗試用exp_ClientMessage來做為Client端傳來訊息的預期答案
另外,感覺上Royce520您對網路傳送檔案有不小的研究,能否再多問您一個問題:nonBlocking與Bolcking方式有何不同,又該如何選擇呢? 附註:事實上,以上程式是小弟在不得已的情況下才改成Client每次收到封包都要回傳訊息的樣子,但是在實際的應用中,Server在收到Client端要求後就只管一直傳送,除非Client端送來停止傳送的訊息才停止,不過如此的方式,小弟試過,傳送檔案會有loss,也請Royce520 或版上的高手多多幫忙…
|
brook
資深會員
發表:57 回覆:323 積分:371 註冊:2002-07-12
發送簡訊給我
|
1.
void TForm1::SendImage0(AnsiString image_name)
{ // 我想這裡可能會個死結,當client端斷線, 你永遠再也收不到任何訊息時???
while (!ClientReceived)
{
Application->ProcessMessages();
StatusBar1->SimpleText = "等待Client回應 :" IntToStr(elaps_time) "/" IntToStr(Timer1->Interval/1000) ;
} }
2. 你要傳檔案(圖片)前, 你還必須check 上一個檔案是否己傳輸完成, 假如還沒完成你又丟, 會發生什麼狀況你應該知道.
|
danielj
初階會員
發表:65 回覆:135 積分:40 註冊:2003-06-11
發送簡訊給我
|
多謝brook的回應!
關於您所提到的第一點,我本來是想說有加入
Application->ProcessMessages();
應該可以讓程式不至於當死在這個迴圈裡,不過您提醒了我,若Client端死了,這個迴圈好像真的會出不去… 而您所提到的第二點,這正是我所困擾的,因為在我的應用中,Server端就只管一直丟檔案(事實上是圖片),可能沒有辦法理會Client端,所以小弟可能不會讓Client端回應任何訊息到Server端(除了開始或停止傳送的指令),也正因為如此,造成小弟一直在嘗試用不回ack的方式傳檔,但是總是會出現錯誤,就算不出現Jpeg error,圖上有時候也會有burst error,還請高手們出手相助,先謝啦!
|
brook
資深會員
發表:57 回覆:323 積分:371 註冊:2002-07-12
發送簡訊給我
|
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=34981
要不然你就試試上面的方式, 用WriteStream(Client,True,False)送
用ReadStream(receive,bytes,False)讀,可能會比較簡單點, 但你還必須處理上一個檔案還沒傳完要等待的問題, 至於能不能解決你的問題, 只能先測看看囉!
要不然, indy 還有個ftp方式傳檔, 你也可試看看.
|
RaynorPao
版主
發表:139 回覆:3622 積分:7025 註冊:2002-08-12
發送簡訊給我
|
danielj 你好:
試試 Indy V9.00.11 元件 TIdTCPServer/TIdTCPClient
------ -- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
|