⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sender1.cpp

📁 文件名称:新曦 我的资源 搜索软件 源程序(Borland Delphi 7)说明
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
    // If any error occured, we just display info and prepare to restart.
    if (Error) {
        MessageBeep(MB_OK);
        Display("DNS failure-> Error #" + IntToStr(Error));
        ActionButton->Caption = "&Start";
        PauseButton->Visible  = FALSE;
        return;
    }

    // Now we know the IP address-> Try to connect.
    WSocket1->Addr  = WSocket1->DnsResult;
    WSocket1->Port  = Trim(PortEdit->Text);
    WSocket1->Proto = "tcp";
    try {
        WSocket1->Connect();
    } catch (const Exception &E) {
        MessageBeep(MB_OK);
        Display("Connect failed: " + E.Message);
        ActionButton->Caption = "&Start";
        PauseButton->Visible  = FALSE;
        FAutoStart            = 0;
        return;
    }
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WSocket1SessionConnected(TObject *Sender,
      WORD Error)
{
    if (Error) {
        MessageBeep(MB_OK);
        Display("Can't connect. Error #" + IntToStr(Error));
        ActionButton->Caption = "&Start";
        FAutoStart            = 0;
        return;
    }
    Display("Connected");
    if (LingerCheckBox->Checked)
        WSocket1->LingerOnOff   = wsLingerOn;
    else
        WSocket1->LingerOnOff   = wsLingerOff;
    WSocket1->LingerTimeout = 300;
    WSocket1->SetLingerOption();
    DoSend();
    if (FUseDataSent)
        return;

    // User requested to not use OnDataSent event-> We will simply loop.
    // until all data has been sent-> This will fill TWSocket internal buffer
    // very quickly while data is being sent in the background at network
    // speed.
    while ((FFinalCount <= 0) || (FFinalCount > FCount)) {
        // We must break the loop if user temrinated the application,
        // or if connection is broke, or if user stopped.
        if (Application->Terminated ||
           (WSocket1->State != wsConnected) ||
           !FSending)
            return;
        // Otherwise, we can send data
        DoSend();
    }
    CountLabel->Caption  = IntToStr(FCount);
    PauseButton->Visible = FALSE;
    Display("All data is in TWSocket buffer and being sent in the background");
    FFinished = TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::DoSend(void)
{
    AnsiString Buf;

    do {
        // Calling ProcessMessages let a chance to button and other events
        // to be handled.
        Application->ProcessMessages();
        // We must stop if the user clicked the close button.
        if (Application->Terminated) {
            Display("Application terminated");
            return;
        }
        // We must stop if the user requested to stop send
        if (!FSending)
            return;
        // We must stop if connection is broken
        if (WSocket1->State != wsConnected)
            return;
        // We don't wants to use 100% CPU just looping. Sleep a little bit
        if (FPaused)
            Sleep(250);
    } while (FPaused);

    // We need to check if we are still connected before sending
    if (WSocket1->State != wsConnected)
        return;

    if ((FFinalCount <= 0) || (FFinalCount > FCount)) {
        // Count the message sent
        FCount++;
        // Put the counter into the message, truvated to 4 digits
        Buf = IntToStr(FCount % 10000) + "    ";
        Move(&Buf[1], &FDataBuf[0], 4);

        // If required, display in memo box (slow down !)
        if (FDisplayData)
            Display("Sending " + IntToStr(FCount));
        // Display the counter every 100 sends
        if ((FCount % 100) == 0)
            CountLabel->Caption = IntToStr(FCount);

        // Try to send data-> Send may fail !
        try {
            WSocket1->Send(FDataBuf, FDataBufSize - 1);
        } catch (const Exception &E) {
            Display("Exception during TWSocket->Send(): " + E.Message);
            FAutoStart = 0;
            PostMessage(Handle, WM_CLOSE_REQUEST, 0, (LPARAM)WSocket1);
        }
    }
    else {
        Display("Required data has been sent. Closing.");
        // We may have not read data send by server-> But anyway, close the
        // session.
        PostMessage(Handle, WM_CLOSE_REQUEST, 0, (LPARAM)WSocket1);
    }
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WSocket1DataSent(TObject *Sender, WORD Error)
{
    DoSend();
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WSocket1NoDataSent(TObject *Sender, WORD Error)
{
    if (FFinished) {
        if (!WSocket1->AllSent)
            Display("Not all sent");
        Display("Required data has been sent. Closing.");
        PostMessage(Handle, WM_CLOSE_REQUEST, 0, (LPARAM)WSocket1);
    }
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WSocket1DataAvailable(TObject *Sender,
      WORD Error)
{
    char     *Buf;
    TWSocket *Cli;
    int      Len;
    int      Cnt;

    Cli = (TWSocket *)Sender;
    Cnt = Cli->RcvdCount;
    if (Cnt <= 0)
        return;

    Buf = (char *)malloc(Cnt + 1);
    try {
        Len = Cli->Receive(Buf, Cnt);
        if (Len > 0) {
            Buf[Cnt] = 0;
            Display("Received: " + (AnsiString)Buf);
        }
    } catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    free(Buf);
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WSocket1SessionClosed(TObject *Sender,
      WORD Error)
{
    if (Error == 0)
        Display("Socket closed, no error");
    else {
        Display("Socket closed, Error #" + IntToStr(Error));
        FAutoStart = 0;
    }
    FSending              = FALSE;
    ActionButton->Caption = "&Start";
    PauseButton->Visible  = FALSE;
    FPaused               = FALSE;
    if (FAutoStart > 0) {
        FAutoStart++;
        AutoStartButton->Caption = IntToStr(FAutoStart);
        PostMessage(Handle, WM_AUTO_START, 0, 0);
    }
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::DisplayDataCheckBoxClick(TObject *Sender)
{
    FDisplayData = DisplayDataCheckBox->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::UseDataSentCheckBoxClick(TObject *Sender)
{
    FUseDataSent = UseDataSentCheckBox->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::PauseButtonClick(TObject *Sender)
{
    CountLabel->Caption = IntToStr(FCount);
    FPaused = !FPaused;
    if (FPaused)
        PauseButton->Caption = "&Resume";
    else
        PauseButton->Caption = "&Pause";
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::AutoStartButtonClick(TObject *Sender)
{
    if (FAutoStart != 0) {
        FAutoStart = 0;
        return;
    }

    FAutoStart = 1;
    AutoStartButton->Caption = IntToStr(FAutoStart);
    PostMessage(Handle, WM_AUTO_START, 0, 0);
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WMCloseRequest(TMessage Msg)
{
    TWSocket *WSocket;

    WSocket = (TWSocket *)(Msg.LParam);
    WSocket->Close();
}
//---------------------------------------------------------------------------
void __fastcall TSenderForm::WMAutoStart(TMessage Msg)
{
    ActionButtonClick(this);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -