Unit1.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, HPSocketSDKUnit;
  6. type
  7. TForm1 = class(TForm)
  8. Label1: TLabel;
  9. Label2: TLabel;
  10. Edit1: TEdit;
  11. Label3: TLabel;
  12. Label4: TLabel;
  13. Edit2: TEdit;
  14. Button1: TButton;
  15. Button2: TButton;
  16. ListBox1: TListBox;
  17. Edit3: TEdit;
  18. Button3: TButton;
  19. procedure FormCreate(Sender: TObject);
  20. procedure Button1Click(Sender: TObject);
  21. procedure Button2Click(Sender: TObject);
  22. procedure Button3Click(Sender: TObject);
  23. private
  24. { Private declarations }
  25. public
  26. { Public declarations }
  27. end;
  28. var
  29. Form1: TForm1;
  30. PClient: Integer;
  31. PListener: Integer;
  32. implementation
  33. {$R *.dfm}
  34. procedure AddMsg(str: string);
  35. begin
  36. Form1.ListBox1.Items.add('==> '+str);
  37. end;
  38. function DoStrToWideChar(s: string): PWideChar;
  39. begin
  40. Result := PWideChar(WideString(s));
  41. end;
  42. function OnPrepareConnect(pClient: HP_Client; SOCKET: Pointer): En_HP_HandleResult; stdcall;
  43. begin
  44. AddMsg('准备连接 -> ' + inttostr(pClient));
  45. Result := HP_HR_OK;
  46. end;
  47. function OnConnect(pClient: HP_Client): En_HP_HandleResult; stdcall;
  48. begin
  49. AddMsg('连接成功 -> ' + inttostr(pClient));
  50. Result := HP_HR_OK;
  51. end;
  52. function OnSend(pClient: HP_Client; const pData: Pointer; iLength: Integer): En_HP_HandleResult; stdcall;
  53. begin
  54. AddMsg('发送数据 -> ' + inttostr(pClient));
  55. Result := HP_HR_OK;
  56. end;
  57. function OnReceive(pClient: HP_Client; const pData: Pointer; iLength: Integer): En_HP_HandleResult; stdcall;
  58. begin
  59. AddMsg('收到数据 -> ' + inttostr(iLength) + ' byte');
  60. Result := HP_HR_OK;
  61. end;
  62. function OnCloseCon(pClient: HP_Client; enOperation: En_HP_SocketOperation; iLength: Integer): En_HP_HandleResult; stdcall;
  63. begin
  64. AddMsg('连接关闭 -> ' + inttostr(pClient) );
  65. Result := HP_HR_OK;
  66. end;
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. begin
  69. //创建监听器
  70. PListener:= Create_HP_TcpClientListener();
  71. //创建服务
  72. PClient:= Create_HP_TcpClient(PListener);
  73. //设置回调函数
  74. HP_Set_FN_Client_OnPrepareConnect(PListener, OnPrepareConnect);
  75. HP_Set_FN_Client_OnConnect(PListener, OnConnect);
  76. HP_Set_FN_Client_OnSend(PListener, OnSend);
  77. HP_Set_FN_Client_OnReceive(PListener, OnReceive);
  78. HP_Set_FN_Client_OnClose(PListener,OnCloseCon);
  79. AddMsg('客户端连接创建成功 -> ');
  80. end;
  81. procedure TForm1.Button1Click(Sender: TObject);
  82. var
  83. Ip:PWideChar;
  84. Port:Word;
  85. begin
  86. ip := DoStrToWideChar(Edit1.Text);
  87. port := StrToInt(Edit2.Text);
  88. if HP_Client_Start(PClient, ip, port, False) then
  89. begin
  90. AddMsg(Format('连接启动成功 -> (%s:%d)', [ip, port]));
  91. Label4.Caption:= '已连接'
  92. end
  93. else
  94. begin
  95. AddMsg('连接启动失败 -> %s(%d)');
  96. end;
  97. end;
  98. procedure TForm1.Button2Click(Sender: TObject);
  99. begin
  100. if HP_Client_Stop(PClient) then
  101. begin
  102. AddMsg('连接关闭成功');
  103. Label4.Caption:= '未连接'
  104. end
  105. else
  106. AddMsg('连接关闭失败');
  107. end;
  108. procedure TForm1.Button3Click(Sender: TObject);
  109. var
  110. s: string;
  111. len: Integer;
  112. begin
  113. s:= Edit3.Text;
  114. len:= Length(s);
  115. HP_Client_Send(PClient, @s, len) ;
  116. end;
  117. end.