unitthread.pas

来自「如何在Delphi下实现画面捕捉、传输、以及文件的传输原理」· PAS 代码 · 共 124 行

PAS
124
字号
unit UnitThread;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,StrUtils;

type

	TCommBlock = record
                StrKind,
        	SendName,
                ToName,
                Msg : String[100];
        end;

  TFmThread = class(TForm)
    IdTCPClient: TIdTCPClient;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TClientHandleThread = class(TThread)
  private
    CB: TCommBlock;
    procedure HandleInput;
  protected
    procedure Execute; override;
  end;

var
  FmThread: TFmThread;
  ClientHandleThread : TClientHandleThread;

implementation

uses UnitChat,UnitLogin,UnitFriendList,UnitRecord;

{$R *.dfm}

procedure TFmThread.FormCreate(Sender: TObject);
begin
	IdTCPClient.Host := '127.0.0.1';
        IdTCPClient.Port := 8080;
	try
        	IdTCPClient.Connect;
                ClientHandleThread := TClientHandleThread.Create(True);///create thread object
    		ClientHandleThread.FreeOnTerminate:=True;
    		ClientHandleThread.Resume;
        except
        	on E : Exception do showmessage('连接失败!' + E.Message);
        end;
end;

Procedure TClientHandleThread.HandleInput;
var
	Str_Friend,Str_Temp : String;
        CountA,CountZ : Integer;
begin
        CountA := 0;
	if CB.StrKind = 'LOGIN' then
        begin
        	if CB.Msg = 'R' then
                begin
                	FmLogin.Hide;
                        FmFriendList.Show;
                end
        	else
                if CB.Msg = 'W'then
                begin
                	showmessage('密码错误!');
                end;
        end
        else
        if CB.StrKind = 'FRDLIST' then
        begin
                Str_Temp := CB.Msg;
                CountZ := Pos('/',Str_Temp); //a=0,z=5

                while CountZ <> 0 do
                begin
                	Str_Friend := Copy(Str_Temp,CountA + 1,CountZ - CountA- 1);
            		FmFriendList.ListBox.Items.Add(Str_Friend);
                        CountA := CountZ;
                        CountZ := PosEX('/',Str_Temp,CountZ + 1);
                end;
        end
        else
        if CB.StrKind = 'SENDMSG' then
        begin
                FmChat.Show;
        	FmChat.MemRecd.Lines.Add(CB.SendName + ': ' + TimeToStr(Time) + 'Say: ' + CB.Msg);
        end;
end;

Procedure TClientHandleThread.Execute;
begin
	while not Terminated do
        begin
        	if not FmThread.IdTCPClient.Connected then
                begin
                	Terminate;
                end
                else
                begin
                	FmThread.IdTCPClient.ReadBuffer(CB,SizeOf(CB));
                        Synchronize(HandleInput);
                end;
        end
end;

procedure TFmThread.FormClose(Sender: TObject; var Action: TCloseAction);
begin
       	ClientHandleThread.Terminate;
        FmThread.IdTCPClient.Disconnect;
end;

end.

⌨️ 快捷键说明

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