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

📄 sender1.pas

📁 文件名称:新曦 我的资源 搜索软件 源程序(Borland Delphi 7)说明
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Author:       Fran鏾is PIETTE
Description:  Simple client program which just send data to a server and display
              all incomming data.
EMail:        francois.piette@pophost.eunet.be    francois.piette@rtfm.be
              http://www.rtfm.be/fpiette
Creation:     Oct 01, 1998
Version:      1.03
Support:      Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1998 by Fran鏾is PIETTE
              Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
              <francois.piette@pophost.eunet.be>

              This software is provided 'as-is', without any express or
              implied warranty.  In no event will the author be held liable
              for any  damages arising from the use of this software.

              Permission is granted to anyone to use this software for any
              purpose, including commercial applications, and to alter it
              and redistribute it freely, subject to the following
              restrictions:

              1. The origin of this software must not be misrepresented,
                 you must not claim that you wrote the original software.
                 If you use this software in a product, an acknowledgment
                 in the product documentation would be appreciated but is
                 not required.

              2. Altered source versions must be plainly marked as such, and
                 must not be misrepresented as being the original software.

              3. This notice may not be removed or altered from any source
                 distribution.

History:
Oct 28, 1998  V1.02 Trapped Connect exceptions.
                    Added AutoStartButton and associated logic.
                    Added LingerCheckBox and associated logic.
Mar 07, 1999  V1.03 Adapted for Delphi 1


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit Sender1;

interface

uses
  WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, IniFiles, WSocket;

const
  WM_AUTO_START      = WM_USER + 1;
  WM_CLOSE_REQUEST   = WM_USER + 2;

type
{$IFDEF VER80}
  LParam = LongInt;
{$ENDIF}
  TSenderForm = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    ServerEdit: TEdit;
    Label2: TLabel;
    PortEdit: TEdit;
    Label3: TLabel;
    DataEdit: TEdit;
    Label4: TLabel;
    RepeatEdit: TEdit;
    ContCheckBox: TCheckBox;
    ActionButton: TButton;
    DisplayMemo: TMemo;
    Label5: TLabel;
    LengthEdit: TEdit;
    WSocket1: TWSocket;
    DisplayDataCheckBox: TCheckBox;
    UseDataSentCheckBox: TCheckBox;
    PauseButton: TButton;
    CountLabel: TLabel;
    AutoStartButton: TButton;
    LingerCheckBox: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ContCheckBoxClick(Sender: TObject);
    procedure WSocket1DnsLookupDone(Sender: TObject; Error: Word);
    procedure WSocket1SessionConnected(Sender: TObject; Error: Word);
    procedure ActionButtonClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure WSocket1DataAvailable(Sender: TObject; Error: Word);
    procedure WSocket1SessionClosed(Sender: TObject; Error: Word);
    procedure DisplayDataCheckBoxClick(Sender: TObject);
    procedure UseDataSentCheckBoxClick(Sender: TObject);
    procedure PauseButtonClick(Sender: TObject);
    procedure AutoStartButtonClick(Sender: TObject);
  private
    FIniFileName : String;
    FInitialized : Boolean;
    FDataBuf     : PChar;
    FDataBufSize : Integer;
    FCount       : Integer;
    FFinalCount  : Integer;
    FSending     : Boolean;
    FDisplayData : Boolean;
    FUseDataSent : Boolean;
    FFinished    : Boolean;
    FPaused      : Boolean;
    FAutoStart   : Integer;
    procedure Display(Msg : String);
    procedure DoSend;
    procedure WSocket1DataSent(Sender: TObject; Error: Word);
    procedure WSocket1NoDataSent(Sender: TObject; Error: Word);
    procedure WMAutoStart(var msg: TMessage); message WM_AUTO_START;
    procedure WMCloseRequest(var msg: TMessage); message WM_CLOSE_REQUEST;
  public
    { D閏larations publiques }
  end;

var
  SenderForm: TSenderForm;

implementation

{$R *.DFM}

const
    SectionWindow   = 'RecvForm';
    KeyTop          = 'Top';
    KeyLeft         = 'Left';
    KeyWidth        = 'Width';
    KeyHeight       = 'Height';
    SectionData     = 'Data';
    KeyPort         = 'Port';
    KeyServer       = 'Server';
    KeyData         = 'Data';
    KeyRepeat       = 'RepeatCount';
    KeyContinuous   = 'ContinuousSend';
    KeyLength       = 'DataLength';
    KeyUseDataSent  = 'UseDataSent';
    KeyDisplay      = 'Display';
    KeyLinger       = 'Linger';


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.FormCreate(Sender: TObject);
begin
    FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
    FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.FormShow(Sender: TObject);
var
    IniFile : TIniFile;
begin
    if not FInitialized then begin
        FInitialized := TRUE;
        IniFile      := TIniFile.Create(FIniFileName);
        Width        := IniFile.ReadInteger(SectionWindow, KeyWidth,  Width);
        Height       := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);
        Top          := IniFile.ReadInteger(SectionWindow, KeyTop,
                                            (Screen.Height - Height) div 2);
        Left         := IniFile.ReadInteger(SectionWindow, KeyLeft,
                                            (Screen.Width  - Width)  div 2);
        PortEdit.Text        := IniFile.ReadString(SectionData, KeyPort, 'telnet');
        ServerEdit.Text      := IniFile.ReadString(SectionData, KeyServer, 'localhost');
        DataEdit.Text        := IniFile.ReadString(SectionData, KeyData,       'The quick brown fox jumps over the lazy dog');
        RepeatEdit.Text      := IniFile.ReadString(SectionData, KeyRepeat,     '');
        LengthEdit.Text      := IniFile.ReadString(SectionData, KeyLength,     '60');
        ContCheckBox.Checked        := Boolean(IniFile.ReadInteger(SectionData, KeyContinuous,  0));
        LingerCheckBox.Checked      := Boolean(IniFile.ReadInteger(SectionData, KeyLinger,      1));
        DisplayDataCheckBox.Checked := Boolean(IniFile.ReadInteger(SectionData, KeyDisplay,     0));
        UseDataSentCheckBox.Checked := Boolean(IniFile.ReadInteger(SectionData, KeyUseDataSent, 1));
        IniFile.Destroy;
        RepeatEdit.Enabled := not ContCheckBox.Checked;
        CountLabel.Caption  := '';
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
    IniFile : TIniFile;
begin
    IniFile := TIniFile.Create(FIniFileName);
    IniFile.WriteInteger(SectionWindow, KeyTop,       Top);
    IniFile.WriteInteger(SectionWindow, KeyLeft,      Left);
    IniFile.WriteInteger(SectionWindow, KeyWidth,     Width);
    IniFile.WriteInteger(SectionWindow, KeyHeight,    Height);
    IniFile.WriteString(SectionData, KeyPort,   PortEdit.text);
    IniFile.WriteString(SectionData, KeyServer, ServerEdit.text);
    IniFile.WriteString(SectionData, KeyData,   DataEdit.text);
    IniFile.WriteString(SectionData, KeyRepeat, RepeatEdit.text);
    IniFile.WriteString(SectionData, KeyLength, LengthEdit.text);
    IniFile.WriteInteger(SectionData, KeyContinuous,  Ord(ContCheckBox.Checked));
    IniFile.WriteInteger(SectionData, KeyLinger,      Ord(LingerCheckBox.Checked));
    IniFile.WriteInteger(SectionData, KeyUseDataSent, Ord(UseDataSentCheckBox.Checked));
    IniFile.WriteInteger(SectionData, KeyDisplay,     Ord(DisplayDataCheckBox.Checked));
    IniFile.Destroy;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.FormDestroy(Sender: TObject);
begin
    if FDataBuf <> nil then begin
        Freemem(FDataBuf, FDataBufSize);
        FDataBuf := nil;
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.ContCheckBoxClick(Sender: TObject);
begin
    RepeatEdit.Enabled := not ContCheckBox.Checked;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.Display(Msg : String);
begin
    if DisplayMemo.lines.Count > 200 then
        DisplayMemo.Clear;
    DisplayMemo.Lines.Add(Msg);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimRight(Str : String) : String;
var
    i : Integer;
begin
    i := Length(Str);
    while (i > 0) and (Str[i] = ' ') do
        i := i - 1;
    Result := Copy(Str, 1, i);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
    i : Integer;
begin
    if Str[1] <> ' ' then
        Result := Str
    else begin
        i := 1;
        while (i <= Length(Str)) and (Str[i] = ' ') do
            i := i + 1;
        Result := Copy(Str, i, Length(Str) - i + 1);
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Trim(Str : String) : String;
begin
    Result := TrimLeft(TrimRight(Str));
end;
{$ENDIF}


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSenderForm.ActionButtonClick(Sender: TObject);
var
    Len : Integer;
    N   : Integer;
    T   : Integer;
    Buf : String;
begin
    { The ActionButton is used to start or stop data transmission }
    if FSending then begin
        { We are already sending, so user wants to stop }
        { Display updated counter                       }
        CountLabel.Caption := IntToStr(FCount);

        { Check if some data remains in TWSocket's internal buffer }
        if (not WSocket1.AllSent) and
           (Application.MessageBox('Data is still being sent' + #10 +
                                   'Close anyway ?',
                                   'Warning', MB_YESNO) <> IDYES) then
            Exit;

        Display('Stop requested');
        if not WSocket1.AllSent then
            Display('Not all data has been sent');

        FAutoStart := 0;
        { Close the socket. This will delete any data not already sent to }
        { winsock.                                                        }
        PostMessage(Handle, WM_CLOSE_REQUEST, 0, LParam(WSocket1));

        Exit;
    end;

    { The user wants to start data transmission }
    CountLabel.Caption   := '';
    PauseButton.Caption  := '&Pause';
    PauseButton.Visible  := TRUE;
    ActionButton.Caption := '&Stop';
    FPaused              := FALSE;
    FSending             := TRUE;
    FFinished            := FALSE;
    FCount               := 0;

    { Setup final count }
    if ContCheckBox.Checked then
        FFinalCount := 0
    else
        FFinalCount := StrToInt(Trim(RepeatEdit.Text));

⌨️ 快捷键说明

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