如何将TServerSocket绑定到特定的IP地址 [英] How to Bind a TServerSocket to a specific IP address

查看:78
本文介绍了如何将TServerSocket绑定到特定的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何绑定Delphi TServerSocket组件以仅接受特定本地地址上的请求吗?

Does any one know any way to bind a Delphi TServerSocket component to accept requests only on a specific local address?

服务器具有多个IP,但要求服务器应用程序仅在运行时才接受一个I​​P上的请求.

The server has several IPs but it is requires that server application to accept requests on one IP only when its running.

推荐答案

TServerSocket不会直接公开您要的功能,但是可以通过一些解决方法来实现.

TServerSocket does not directly expose the feature you are asking for, however it is doable with a little workaround.

您需要从TServerSocket派生一个新类,以便访问受保护的TAbstractSocket.Address属性.这是TServerSocket绑定到的值.由于通常无法访问该属性,因此该属性保留为空字符串,与绑定到0.0.0.0(也称为INADDR_ANY,即所有本地IP)相同.

You need to derive a new class from TServerSocket in order to gain access to the protected TAbstractSocket.Address property. That is the value that TServerSocket binds to. Since the property is not normally accessible, it remains an empty string, which is the same as binding to 0.0.0.0 (aka INADDR_ANY, ie all local IPs).

一旦您可以访问Address属性,就可以将其设置为激活服务器之前所需的任何IP,服务器将进行相应的绑定.

Once you can access the Address property, you can set it to whatever IP you want prior to activating the server, and the server will bind accordingly.

例如:

type
  TServerSocketAccess = class(TServerSocket)
  end;

TServerSocketAccess(ServerSocket1).Address := '192.168.0.1';
ServerSocket1.Active := True;

这篇关于如何将TServerSocket绑定到特定的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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