IdTCPServer和IdTCPClient的問題 |
答題得分者是:sryang
|
pchw8598
一般會員 ![]() ![]() 發表:43 回覆:63 積分:21 註冊:2003-11-09 發送簡訊給我 |
各位好我剛由BCB6轉移到XE3,很多VCL原件用起來都怪怪的不上手,以下簡單的Server-Client程式碼就很奇怪,請大家幫幫忙,謝謝. Server和Client都啟動後: 問題1. Client連上了Server之後,再由Client斷線, 然後就再也連不上Server了 問題2. Client連線時,關閉Server,Server就當了 程式碼如下(不好意思,我不會貼程式): Server端: //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Server.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; bool fgActive; //伺服器是否啟動 int ServerPort; //連接埠 //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) { return; { return; : TForm(Owner) { fgConnected=false; StatusBar1->Panels->Items[0]->Text="未連接伺服器"; Edit1->Text=IdIPWatch1->LocalIP(); ServerIP=Edit1->Text; ServerPort=StrToInt(Edit2->Text); fgConnected=false; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if(fgConnected) { return; { IdTCPClient1->Host=ServerIP; IdTCPClient1->Port=ServerPort; } catch(...) { ShowMessage("無法與伺服器連線"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPClient1Connected(TObject *Sender) { Memo1->Lines->Add("已連上伺服器"); fgConnected=true; StatusBar1->Panels->Items[0]->Text="已連線"; Memo1->Lines->Add(IdTCPClient1->IOHandler->ReadLn()); } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPClient1Disconnected(TObject *Sender) { Memo1->Lines->Add("伺服器已斷線"); fgConnected=false; StatusBar1->Panels->Items[0]->Text="未連線"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if(!fgConnected) ShowMessage("尚未連線"); return; } IdTCPClient1->Disconnect(); Memo1->Lines->Add("Client斷線," Time() "," Date()); StatusBar1->Panels->Items[0]->Text="伺服器已斷線"; fgConnected=false; } //--------------------------------------------------------------------------- |
sryang
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
|
pchw8598
一般會員 ![]() ![]() 發表:43 回覆:63 積分:21 註冊:2003-11-09 發送簡訊給我 |
|
pchw8598
一般會員 ![]() ![]() 發表:43 回覆:63 積分:21 註冊:2003-11-09 發送簡訊給我 |
感謝sryang的回覆, 現在只剩下一個問題: 當server和client互相連著的時候, server自行斷線(就是 avtive設為false) 時, server 程式就當了, 附上程式原始碼, 還請各位大大幫忙~謝謝. Server: //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Server.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) { return; { return; for (int index = 0; index < List->Count; index ) { } } __finally { : TForm(Owner) { fgConnected=false; StatusBar1->Panels->Items[0]->Text="未連接伺服器"; Edit1->Text=IdIPWatch1->LocalIP(); ServerIP=Edit1->Text; ServerPort=StrToInt(Edit2->Text); fgConnected=false; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if(fgConnected) { return; { IdTCPClient1->Host=ServerIP; IdTCPClient1->Port=ServerPort; } catch(...) { ShowMessage("無法與伺服器連線"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPClient1Connected(TObject *Sender) { Memo1->Lines->Add("已連上伺服器"); fgConnected=true; StatusBar1->Panels->Items[0]->Text="已連線"; Memo1->Lines->Add(IdTCPClient1->IOHandler->ReadLn()); } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPClient1Disconnected(TObject *Sender) { Memo1->Lines->Add("伺服器已斷線"); fgConnected=false; StatusBar1->Panels->Items[0]->Text="未連線"; IdTCPClient1->Disconnect(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if(!fgConnected) ShowMessage("尚未連線"); return; } IdTCPClient1->Disconnect(); Memo1->Lines->Add("Client斷線," Time() "," Date()); StatusBar1->Panels->Items[0]->Text="伺服器已斷線"; fgConnected=false; } //--------------------------------------------------------------------------- |
aftcast
站務副站長 ![]() ![]() ![]() ![]() ![]() 發表:81 回覆:1485 積分:1763 註冊:2002-11-21 發送簡訊給我 |
------
蕭沖 --All ideas are worthless unless implemented-- C++ Builder Delphi Taiwan G+ 社群 http://bit.ly/cbtaiwan |
pchw8598
一般會員 ![]() ![]() 發表:43 回覆:63 積分:21 註冊:2003-11-09 發送簡訊給我 |
|
pchw8598
一般會員 ![]() ![]() 發表:43 回覆:63 積分:21 註冊:2003-11-09 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |