简体   繁体   English

Indy 10 TCP客户端服务器 - 测试开放式通信通道

[英]Indy 10 TCP Client Server - testing for open communication channel

I am modifying an Indy10 TCP/IP application and I would like your suggestions/opinions/sample code on implementing a client side function that does the following 我正在修改Indy10 TCP / IP应用程序,我希望您的建议/意见/示例代码实现客户端功能,执行以下操作

a) on application startup when the splash screen is displayed, it verifies that the client computer has internet access and the TCP Server is up and running and waiting for communication. a)在显示启动画面时应用程序启动时,它会验证客户端计算机是否具有Internet访问权限,并且TCP服务器已启动并正在运行并等待通信。 If this is not the case, the application should terminate. 如果不是这种情况,应该终止申请。

b) does (a) above before ANY data exchange between the client and the server b)在客户端和服务器之间进行任何数据交换之前,执行上述(a)

In addition, does the server need to repeatedly broadcast some sort of message to inform potential clients that it is up and running? 此外,服务器是否需要重复广播某种消息以通知潜在客户它已启动并运行?

Thanks for your assistance. 谢谢你的协助。

How to verify if it's possible to connect to a TCP server ? 如何验证是否可以连接到TCP服务器?

To your first question; 对你的第一个问题; definitely wrap the connection attempt to a separate thread, which you'll run when your splash screen shows. 肯定将连接尝试包装到一个单独的线程,您将在启动屏幕显示时运行该线程。 In that thread you can simply try to Connect and catch the exception. 在该线程中,您只需尝试Connect并捕获异常即可。 If the exception is raised, the connection failed. 如果引发异常,则连接失败。 If not, you were able to connect. 如果没有,你就可以连接。 For the notification about this state I would use custom messages, which you'll send to a splash screen form like shown in the following pseudocode: 对于有关此状态的通知,我将使用自定义消息,您将发送到启动屏幕表单,如下面的伪代码所示:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdTCPClient;

const
  WM_CONNECTION_NOTIFY = WM_USER + 1;
  SC_CONNECTION_FAILURE = 0;
  SC_CONNECTION_SUCCESS = 1;

type
  TConnThread = class(TThread)
  private
    FMsgHandler: HWND;
    FTCPClient: TIdTCPClient;
  protected
    procedure Execute; override;
  public
    constructor Create(const AHost: string; APort: Word; ATimeout: Integer;
      AMsgHandler: HWND); reintroduce;
    destructor Destroy; override;
  end;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FConnThread: TConnThread;
    procedure WMConnectionNotify(var AMessage: TMessage); message WM_CONNECTION_NOTIFY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TConnThread }

constructor TConnThread.Create(const AHost: string; APort: Word;
  ATimeout: Integer; AMsgHandler: HWND);
begin
  inherited Create(False);
  FreeOnTerminate := False;
  FMsgHandler := AMsgHandler;
  FTCPClient := TIdTCPClient.Create(nil);
  FTCPClient.Host := AHost;
  FTCPClient.Port := APort;
  FTCPClient.ConnectTimeout := ATimeout;
end;

destructor TConnThread.Destroy;
begin
  FTCPClient.Free;
  inherited;
end;

procedure TConnThread.Execute;
begin
  try
    FTCPClient.Connect;
    PostMessage(FMsgHandler, WM_CONNECTION_NOTIFY, 0, SC_CONNECTION_SUCCESS);
  except
    PostMessage(FMsgHandler, WM_CONNECTION_NOTIFY, 0, SC_CONNECTION_FAILURE);
  end;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  FConnThread := TConnThread.Create('123.4.5.6', 123, 5000, Handle);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FConnThread.Free;
end;

procedure TForm1.WMConnectionNotify(var AMessage: TMessage);
begin
  case AMessage.LParam of
    // the connection failed
    SC_CONNECTION_FAILURE: ;
    // the connection succeeded
    SC_CONNECTION_SUCCESS: ;
  end;
end;

end.

Does the server need to repeatedly broadcast some sort of message to inform potential clients that is running ? 服务器是否需要重复广播某种消息以通知正在运行的潜在客户端?

No, this works in a different direction - client asks server if it's running. 不,这可以在不同的方向工作 - 客户端询问服务器是否正在运行。 It's like that simply because server doesn't know clients, but client knows server. 这就是因为服务器不知道客户端,但客户端知道服务器。

On "does the server need to repeatedly broadcast some sort of message" : “服务器是否需要重复播放某种消息”

There are systems (servers, services) which advertise their location (IP adress, port number) and even additional information (for example a status) to interested clients actively using IP Multicast . 有些系统(服务器,服务)使用IP多播主动向感兴趣的客户通告其位置 (IP地址,端口号)甚至附加信息(例如状态)。

It is easy to implement both server- and client side with Internet Direct (Indy) UDP components. 使用Internet Direct(Indy)UDP组件很容易实现服务器端和客户端端。

Here is a IP multicast example for Delphi for the open source message broker Apache ActiveMQ with full source code: 以下是Delphi的开源消息代理Apache ActiveMQ的IP多播示例,其中包含完整源代码:

Discover ActiveMQ brokers with Delphi XE4 and Indy 10.6 使用Delphi XE4和Indy 10.6发现ActiveMQ代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM