#include
#include
#pragma hdrstop
#include "Unit1.h"
#include "Global.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TrigBtnClick(TObject *Sender)
{
MainForm->IdHTTPServer1->DefaultPort = PortEdit->Text.ToInt();
if (PortEdit->Text.ToInt()!=443)
MainForm->IdHTTPServer1->Intercept = NULL;
MainForm->iRequestCnt = 0;
MainForm->iMaxRequest = 0;
MainForm->iUnitTime = CountEdit->Text.ToInt()*60;
MainForm->iTimeCnt = 0;
Timer1->Interval = MainForm->iUnitTime*1000;
Timer1->Enabled = true;
IdHTTPServer1->Active = true;
MsgMemo->Lines->Add("*****開始listen*****");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StopBtnClick(TObject *Sender)
{
IdHTTPServer1->Active = false;
Timer1->Enabled = false;
MsgMemo->Lines->Add("*****停止listen*****");
MsgMemo->Lines->Add("單位時間(sec):"+AnsiString(MainForm->iUnitTime));
MsgMemo->Lines->Add("已服務要求數:"+AnsiString(MainForm->iRequestCnt));
MsgMemo->Lines->Add("累計時間(min):"+AnsiString(MainForm->iTimeCnt));
MsgMemo->Lines->Add("共服務要求數:"+AnsiString(MainForm->iMaxRequest));
if (MainForm->SaveDialog1->Execute())
{
MainForm->MsgMemo->Lines->SaveToFile(MainForm->SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AddCnt(void)
{
MainForm->iRequestCnt++;
MainForm->iMaxRequest++;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdHTTPServer1CommandGet(TIdPeerThread *AThread,
TIdHTTPRequestInfo *RequestInfo, TIdHTTPResponseInfo *ResponseInfo)
{
// MsgMemo->Lines->Add("CommandGet");
ResponseInfo->ContentText = "a";
AThread->Synchronize(AddCnt);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdServerInterceptOpenSSL1GetPassword(
AnsiString &Password)
{
Password = PASSWORD;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
char cPath[MAX_PATH_LENGTH];
::GetCurrentDirectory(MAX_PATH_LENGTH,cPath);
MainForm->sCurrentPath = AnsiString(cPath);
IdServerInterceptOpenSSL1->SSLOptions->CertFile = MainForm->sCurrentPath + CERTIFICATE_KEY;
IdServerInterceptOpenSSL1->SSLOptions->RootCertFile = MainForm->sCurrentPath + CERTIFICATE_KEY;
IdServerInterceptOpenSSL1->SSLOptions->KeyFile = MainForm->sCurrentPath + PRIVATE_KEY;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
// 到達單位時間,顯示服務要求數
MainForm->iTimeCnt++;
MsgMemo->Lines->Add("*********************************************");
MsgMemo->Lines->Add("單位時間(sec):"+AnsiString(MainForm->iUnitTime));
MsgMemo->Lines->Add("已服務要求數:"+AnsiString(MainForm->iRequestCnt));
MsgMemo->Lines->Add("累計時間(min):"+AnsiString(MainForm->iTimeCnt));
MsgMemo->Lines->Add("共服務要求數:"+AnsiString(MainForm->iMaxRequest));
MainForm->iRequestCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ThreadException(TObject *Sender,Exception *e)
{
MsgMemo->Lines->Add(e->Message);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdHTTPServer1Connect(TIdPeerThread *AThread)
{
// MsgMemo->Lines->Add("Connect");
AThread->OnException = ThreadException;
}
//---------------------------------------------------------------------------
以下是Client端
#include
#include
#pragma hdrstop
#include "Unit1.h"
#include "Global.h"
#include "ConnectThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
MainForm->sURL = DEFAULT_URL;
MainForm->tpResponseStream = new TStringStream(MainForm->sResponse);
MainForm->sRequest = new TStringList;
MainForm->iRequestCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ConnectBtnClick(TObject *Sender)
{
int iMaxThreads,iCnt;
MainForm->sURL = MainForm->URL_Edit->Text;
MainForm->sRequest->Text = MainForm->RequestEdit->Text;
MainForm->iPort = PortEdit->Text.ToInt();
IdHTTP1->Host = MainForm->sURL;
IdHTTP1->Port = MainForm->iPort;
if (MainForm->iPort == 443)
{
MainForm->bHTTPsFlag = true;
IdConnectionInterceptOpenSSL1->SSLOptions->Method = sslvSSLv23;
IdConnectionInterceptOpenSSL1->SSLOptions->Mode = sslmClient;
IdConnectionInterceptOpenSSL1->SSLOptions->VerifyDepth = 0;
// IdHTTP1->InterceptEnabled = false;
IdHTTP1->Intercept = IdConnectionInterceptOpenSSL1;
IdHTTP1->InterceptEnabled = true;
}
else
{
MainForm->bHTTPsFlag = false;
IdHTTP1->InterceptEnabled = false;;
IdHTTP1->Intercept = NULL;
}
iMaxThreads = MainForm->ThreadEdit->Text.ToInt();
Timer1->Interval = TimeEdit->Text.ToInt()*60000;
Timer1->Enabled = true;
while(1)
{
for (iCnt=0;iCnt
{
IdHTTP1->Post(MainForm->sURL,MainForm->sRequest,(TStream *)MainForm->tpResponseStream);
MainForm->iRequestCnt++;
MainForm->MsgMemo->Lines->Add("第"+AnsiString(MainForm->iRequestCnt)+"個連線要求");
// 處理系統Message佇列
Application->ProcessMessages();
}
if (!MainForm->LoopRadioBtn->Checked) break;
}
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
MainForm->LoopRadioBtn->Checked = false;
delete MainForm->tpResponseStream;
delete MainForm->sRequest;
// Application->Terminate();
}
//---------------------------------------------------------------------------
歡迎指教...謝謝...^^
#include
#pragma hdrstop
#include "Unit1.h"
#include "Global.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TrigBtnClick(TObject *Sender)
{
MainForm->IdHTTPServer1->DefaultPort = PortEdit->Text.ToInt();
if (PortEdit->Text.ToInt()!=443)
MainForm->IdHTTPServer1->Intercept = NULL;
MainForm->iRequestCnt = 0;
MainForm->iMaxRequest = 0;
MainForm->iUnitTime = CountEdit->Text.ToInt()*60;
MainForm->iTimeCnt = 0;
Timer1->Interval = MainForm->iUnitTime*1000;
Timer1->Enabled = true;
IdHTTPServer1->Active = true;
MsgMemo->Lines->Add("*****開始listen*****");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StopBtnClick(TObject *Sender)
{
IdHTTPServer1->Active = false;
Timer1->Enabled = false;
MsgMemo->Lines->Add("*****停止listen*****");
MsgMemo->Lines->Add("單位時間(sec):"+AnsiString(MainForm->iUnitTime));
MsgMemo->Lines->Add("已服務要求數:"+AnsiString(MainForm->iRequestCnt));
MsgMemo->Lines->Add("累計時間(min):"+AnsiString(MainForm->iTimeCnt));
MsgMemo->Lines->Add("共服務要求數:"+AnsiString(MainForm->iMaxRequest));
if (MainForm->SaveDialog1->Execute())
{
MainForm->MsgMemo->Lines->SaveToFile(MainForm->SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AddCnt(void)
{
MainForm->iRequestCnt++;
MainForm->iMaxRequest++;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdHTTPServer1CommandGet(TIdPeerThread *AThread,
TIdHTTPRequestInfo *RequestInfo, TIdHTTPResponseInfo *ResponseInfo)
{
// MsgMemo->Lines->Add("CommandGet");
ResponseInfo->ContentText = "a";
AThread->Synchronize(AddCnt);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdServerInterceptOpenSSL1GetPassword(
AnsiString &Password)
{
Password = PASSWORD;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
char cPath[MAX_PATH_LENGTH];
::GetCurrentDirectory(MAX_PATH_LENGTH,cPath);
MainForm->sCurrentPath = AnsiString(cPath);
IdServerInterceptOpenSSL1->SSLOptions->CertFile = MainForm->sCurrentPath + CERTIFICATE_KEY;
IdServerInterceptOpenSSL1->SSLOptions->RootCertFile = MainForm->sCurrentPath + CERTIFICATE_KEY;
IdServerInterceptOpenSSL1->SSLOptions->KeyFile = MainForm->sCurrentPath + PRIVATE_KEY;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
// 到達單位時間,顯示服務要求數
MainForm->iTimeCnt++;
MsgMemo->Lines->Add("*********************************************");
MsgMemo->Lines->Add("單位時間(sec):"+AnsiString(MainForm->iUnitTime));
MsgMemo->Lines->Add("已服務要求數:"+AnsiString(MainForm->iRequestCnt));
MsgMemo->Lines->Add("累計時間(min):"+AnsiString(MainForm->iTimeCnt));
MsgMemo->Lines->Add("共服務要求數:"+AnsiString(MainForm->iMaxRequest));
MainForm->iRequestCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ThreadException(TObject *Sender,Exception *e)
{
MsgMemo->Lines->Add(e->Message);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::IdHTTPServer1Connect(TIdPeerThread *AThread)
{
// MsgMemo->Lines->Add("Connect");
AThread->OnException = ThreadException;
}
//---------------------------------------------------------------------------
以下是Client端
#include
#include
#pragma hdrstop
#include "Unit1.h"
#include "Global.h"
#include "ConnectThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
MainForm->sURL = DEFAULT_URL;
MainForm->tpResponseStream = new TStringStream(MainForm->sResponse);
MainForm->sRequest = new TStringList;
MainForm->iRequestCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ConnectBtnClick(TObject *Sender)
{
int iMaxThreads,iCnt;
MainForm->sURL = MainForm->URL_Edit->Text;
MainForm->sRequest->Text = MainForm->RequestEdit->Text;
MainForm->iPort = PortEdit->Text.ToInt();
IdHTTP1->Host = MainForm->sURL;
IdHTTP1->Port = MainForm->iPort;
if (MainForm->iPort == 443)
{
MainForm->bHTTPsFlag = true;
IdConnectionInterceptOpenSSL1->SSLOptions->Method = sslvSSLv23;
IdConnectionInterceptOpenSSL1->SSLOptions->Mode = sslmClient;
IdConnectionInterceptOpenSSL1->SSLOptions->VerifyDepth = 0;
// IdHTTP1->InterceptEnabled = false;
IdHTTP1->Intercept = IdConnectionInterceptOpenSSL1;
IdHTTP1->InterceptEnabled = true;
}
else
{
MainForm->bHTTPsFlag = false;
IdHTTP1->InterceptEnabled = false;;
IdHTTP1->Intercept = NULL;
}
iMaxThreads = MainForm->ThreadEdit->Text.ToInt();
Timer1->Interval = TimeEdit->Text.ToInt()*60000;
Timer1->Enabled = true;
while(1)
{
for (iCnt=0;iCnt
{
IdHTTP1->Post(MainForm->sURL,MainForm->sRequest,(TStream *)MainForm->tpResponseStream);
MainForm->iRequestCnt++;
MainForm->MsgMemo->Lines->Add("第"+AnsiString(MainForm->iRequestCnt)+"個連線要求");
// 處理系統Message佇列
Application->ProcessMessages();
}
if (!MainForm->LoopRadioBtn->Checked) break;
}
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
MainForm->LoopRadioBtn->Checked = false;
delete MainForm->tpResponseStream;
delete MainForm->sRequest;
// Application->Terminate();
}
//---------------------------------------------------------------------------
歡迎指教...謝謝...^^