使用Delphi-Telnet样式检查远程端口访问 [英] Check remote port access using Delphi - Telnet style

查看:321
本文介绍了使用Delphi-Telnet样式检查远程端口访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将应用程序部署在遭受防火墙严重破坏的环境中。我经常使用Telnet来检查端口是否在网络中打开并且可以访问。
现在,我想在Delphi中实现命令 Telnet [域名或ip] [端口] 的等效功能。

I deploy my application in environments heavily stricken with firewalls. Frequently I find myself using Telnet to check if a port is open and accessible in the network. Now I would like to implement an equivalent functionality of the command, Telnet [domainname or ip] [port], in Delphi.


  1. 仅尝试打开和关闭TCP / IP套接字而不发送或接收任何数据就足够了吗?

  2. 有没有可能使对方监听的任意应用程序/服务崩溃的风险?

这是我的代码:

function IsPortActive(AHost : string; APort : Word):boolean;
var IdTCPClient : TIdTCPClient;
begin
  IdTCPClient := TIdTCPClient.Create(nil);
  try
    try
      IdTCPClient.Host := AHost;
      IdTCPClient.Port := APort;
      IdTCPClient.Connect;

    except
      //Igonre exceptions
    end;

  finally
    result := IdTCPClient.Connected;
    IdTCPClient.Disconnect;
    FreeAndNil(IdTCPClient);
  end;
end;


推荐答案

如果只想检查端口是否打开,则可以使用此函数:

If you just want to check whether the port is open, then you can use this:

function IsPortActive(AHost : string; APort : Word): boolean;
var
  IdTCPClient : TIdTCPClient;
begin
  Result := False;
  try
    IdTCPClient := TIdTCPClient.Create(nil);
    try
      IdTCPClient.Host := AHost;
      IdTCPClient.Port := APort;
      IdTCPClient.Connect;
      Result := True;
    finally
      IdTCPClient.Free;
    end;
  except
    //Ignore exceptions
  end;
end;

但这只能告诉您 any 服务器应用程序是否已打开端口。如果要确保服务器应用程序打开了端口,则必须与服务器进行实际通信,并确保其响应符合您的期望。因此,许多常见的服务器协议都会提供初始问候语,以便客户端可以识别与其连接的服务器的类型。如果您可以自由更改通信协议,则可以考虑向服务器添加类似的问候语。

But that only tells you if any server app has opened the port. If you want to make sure that YOUR server app opened the port, then you will have to actually communicate with the server and make sure its responses are what you are expecting. For this reason, many common server protocols provide an initial greeting so clients can identify the type of server they are connected to. You might consider adding a similar greeting to your server, if you are at liberty to make changes to your communication protocol.

仅打开与服务器的连接不会产生任何问候。有导致服务器崩溃的风险,它所做的只是暂时在服务器的客户端列表中占用一个插槽。但是,如果您实际将数据发送到服务器,而您连接的服务器应用程序不是您的应用程序,那么如果服务器无法处理不符合要求的任意数据,则确实会造成 small 风险它的预期协议。但这很少见。发送小命令并不罕见,而且通常很安全,您要么返回答复(其格式可能不符合您的协议,所以假设失败),或者您根本不会收到任何答复(例如,如果服务器正在等待更多数据,或者仅仅是没有被设计为返回答复),在这种情况下,您可以使读取超时并假定失败。

Simply opening a connection to the server does not impose any risk of crashing the server, all it does is momentarily occupy a slot in the server's client list. However, if you actually send data to the server, and the server app you are connected to is not your app, then you do run a small risk if the server cannot handle arbitrary data that does not conform it its expected protocol. But that is pretty rare. Sending a small command is not uncommon and usually pretty safe, you will either get back a reply (which may be in a format that does not conform to your protocol, so just assume failure), or you may not get any reply at all (like if the server is waiting for more data, or simply is not designed to return a reply) in which case you can simply time out the reading and assume failure.

这篇关于使用Delphi-Telnet样式检查远程端口访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆