Delphi多线程和indy10 tcp的使用。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer,
  IdTCPServer, IdTCPConnection, IdTCPClient;

type
  TForm4 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    IdTCPServer1: TIdTCPServer;
    Button3: TButton;
    Memo1: TMemo;
    IdTCPClient1: TIdTCPClient;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TMyThread = class(TThread)
  protected
    procedure Execute; override;
  end;

  TSMSHead = packed record
    SequenceID: Integer;
    CommandID: Integer;
    TotalLength: Integer;
  end;

  PSMSHead = ^TSMSHead;

  TSMSConnect = packed record
    Head: TSMSHead;
    UID: array [0 .. 19] of char;
    PWD: array [0 .. 19] of char;
    Version: Byte;
  end;

  PSMSConnect = ^TSMSConnect;

var
  Form4: TForm4;
  CS: TRTLCriticalSection;

implementation

{$R *.dfm}

function MyFun(p: Pointer): Integer; stdcall;
var
  i: Integer;
begin
  EnterCriticalSection(CS);
  for i := 0 to 500 do
  begin
    Form4.Canvas.Lock;
    Form4.Canvas.TextOut(10, 10,
      'threadid=' + IntToStr(GetCurrentThreadId()) + ',' + IntToStr(i));
    Form4.Canvas.Unlock;
    Sleep(10);
  end;

  Result := 0;
  LeaveCriticalSection(CS);
end;

procedure TForm4.Button1Click(Sender: TObject);
var
  ID: THandle;
begin
  CreateThread(nil, 0, @MyFun, nil, 0, ID);
  CreateThread(nil, 0, @MyFun, nil, 0, ID);
  CreateThread(nil, 0, @MyFun, nil, 0, ID);
  CreateThread(nil, 0, @MyFun, nil, 0, ID);
end;

{ TMyThread }

procedure TMyThread.Execute;
var
  i: Integer;
begin
  FreeOnTerminate := True; { 这可以让线程执行完毕后随即释放 }
  EnterCriticalSection(CS);
  for i := 0 to 500 do
  begin
    Form4.Canvas.Lock;
    Form4.Canvas.TextOut(100, 100,
      'threadid=' + IntToStr(GetCurrentThreadId()) + ',' + IntToStr(i));
    Form4.Canvas.Unlock;
    Sleep(10);
  end;
  LeaveCriticalSection(CS);
end;

procedure TForm4.Button2Click(Sender: TObject);
var
  mythread: TMyThread;
begin
  mythread := TMyThread.Create(True);
  mythread.Start;

  mythread := TMyThread.Create(True);
  mythread.Start;
  mythread := TMyThread.Create(True);
  mythread.Start;

  mythread := TMyThread.Create(True);
  mythread.Start;

end;

procedure TForm4.Button3Click(Sender: TObject);
begin
  IdTCPServer1.DefaultPort := 9999;
  IdTCPServer1.Active := True;
end;

procedure TForm4.Button4Click(Sender: TObject);
begin
  IdTCPClient1.Host := '127.0.0.1';
  IdTCPClient1.Port := 9999;
  // IdTCPClient1.Connect;
end;

procedure TForm4.Button5Click(Sender: TObject);
var
  pkt: TSMSConnect;
  buf: TBytes;
begin
  // 填写pkt
  pkt.Head.SequenceID := 1000;
  pkt.Head.CommandID := 119;
  StrLCopy(pkt.UID, 'I love you ', SizeOf(pkt.UID));
  StrLCopy(pkt.PWD, 'I love you 你的密码 ', SizeOf(pkt.PWD));
  // 准备缓冲区数据
  SetLength(buf, SizeOf(TSMSConnect));
  Move(pkt, buf[0], SizeOf(TSMSConnect));

  IdTCPClient1.Connect;
  IdTCPClient1.Socket.Write(buf);
  IdTCPClient1.Disconnect;
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  InitializeCriticalSection(CS);
end;

procedure TForm4.FormDestroy(Sender: TObject);
begin
  DeleteCriticalSection(CS);
end;

//http://www.ylzx8.cn/windows/delphi/56833.html
//http://www.cnblogs.com/railgunman/archive/2010/08/31/1814112.html
//http://blog.csdn.net/lee576/article/details/3235956
procedure TForm4.IdTCPServer1Execute(AContext: TIdContext);
var
  pkt: PSMSConnect;
  buf: TBytes;
begin
  SetLength(buf, SizeOf(TSMSConnect));
  AContext.Connection.Socket.ReadBytes(buf, SizeOf(TSMSConnect), False);

  pkt := PSMSConnect(@buf[0]);
  Assert(pkt.Head.SequenceID = 1000);

  Memo1.Lines.Add(pkt.UID);
  Memo1.Lines.Add(pkt.PWD);
end;

end.

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
indy10.2.3 full 完全安装版 D7~2010 版本:indy10.2.3 经delphi7下的安装成功,delphi11和delphi6的安装没有试用 【自动安装】 Lib\Fulld7.bat 双击运行即可。 自动会把bpl文件拷到C:\windows\system32 重新打开delphi7会自动加载 dclIndyCore70.bpl dclIndyProtocols70.bpl 【手工安装】 1.windows的path路径中增加D7的路径 在启用delphi7时,要能加载dclIndyCore70.bpl这个文件 如果是自动安装,这个文件会拷到C:\windows\system32\dclIndyCore70.bpl,所以不用改path 2.delphi7菜单中Library路径中 添加indy10\LIB路径下的System、Core、Protocols下的三个目录 添加indey10\D7 因为这个目录下有bpl和dcu文件 3.删除delphi7目录Bin下的indy*.BPL文件,这是官方旧版本的文件,放心删除。 4.删除delphi7目录下的 Id*.DCU文件,一般都是在lib目录里。 5.打开indy10\lib目录,安装DPK包顺序如下: (1)编译 System\IndySystem70.dpk (只需要compile) (这个不是设计包,不需要install) (2)编译 Core\IndyCore70.dpk (只需要compile) 编译安装Core\dclIndyCore70.dpk (设计包,先complie后install) (3)编译 Protocols\IndyProtocols70.dpk (只需要compile) 编译安装Protocols\dclIndyProtocols70.dpk (设计包,先complie后install) 6.完成 【第二次手工安装】 第一步: indy10\D7目录下的5个文件 复制到delphiD:\Delphi7_Ent\Projects\Bpl IndySystem70.bpl IndyCore70.bpl IndyProtocols70.bpl dclIndyCore70.bpl dclIndyProtocols70.bpl 第二步: 打开delphi7菜单 Component/install packpages/ add dclIndyCore70.bpl 再 add dclIndyProtocols70.bpl 第三步 打开delphi7菜单中Library路径中 添加indy10\LIB路径下的System、Core、Protocols下的三个目录 完成!更简单。前提是要有现成bpl的文件,如果没有bpl文件,还得需要dpk编译

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值