TIdHttpServer端

 #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();
}
//---------------------------------------------------------------------------

歡迎指教...謝謝...^^
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值