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

📄 msgclient.~pas

📁 使用delphi自带的控件
💻 ~PAS
字号:
unit MsgClient;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms,
  Dialogs, DBTables, DB, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient;

type
  TdmeMsgClient = class(TDataModule)
    tcpMsgClient: TIdTCPClient;

  private
    { Private declarations }
  public
    { Public declarations }
    function Connect(Server: string; Port: Integer): Boolean;
    //function Connect(): Boolean;
    function TalkWithServer(Command: string; Res: TStringList): Boolean;
  end;

var
  dmeMsgClient: TdmeMsgClient;

implementation

{$R *.dfm}

{**********************************
 判断是否已经建立与服务器的连接,
 如果已经建立,则通过调用Disconnect
 主动断开连接。指定需要连接的地址和
 端口后,再建立与服务器的连接方法
**********************************}
function TdmeMsgClient.Connect(Server: string; Port: Integer): Boolean;
//function TdmeMsgClient.Connect(): Boolean;
begin
  try
    if (tcpMsgClient.Connected) then
      tcpMsgClient.Disconnect;
  except;
  end;

  try
    tcpMsgClient.Host := Server;
    tcpMsgClient.Port := Port;
    tcpMsgClient.Connect;
    Result := True;
  except
    Result := False;
  end;
end;




function TdmeMsgClient.TalkWithServer(Command: string; Res: TStringList): Boolean;
var
  Line: string;
begin
  if (not tcpMsgClient.Connected) then
  begin
    Result := False;
    Exit;
  end;

  try
    //send the request
    tcpMsgClient.WriteLn(Command);
    tcpMsgClient.WriteLn('.');
    //get the result
    Res.Clear;
    Line := tcpMsgClient.ReadLn;
    while ((Line<>'.') and (Line<>' ')) do
    begin
      Res.Add(Line);
      Line := tcpMsgClient.ReadLn;
    end;
    Result := True;
  except
  Result := False;
  end;
end;


end.

⌨️ 快捷键说明

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