对这段IdTCPServer的程序不理解!(5分)

  • 主题发起人 主题发起人 16cy
  • 开始时间 开始时间
1

16cy

Unregistered / Unconfirmed
GUEST, unregistred user!
这段程序有点不理解:
var
Binding : TIdSocketHandle;
begin
...
for i := 0 to lbIPs.Count-1 do
if lbIPs.Checked then
begin
Binding := IdTCPServer.Bindings.Add;
Binding.IP := lbIPs.Items.Strings;
Binding.Port := StrToInt(edtPort.Text);
SL.append('Server bound to IP ' + Binding.IP + ' on port ' + edtPort.Text);
end;

IdTCPServer.Active := true;
result := IdTCPServer.Active;
fServerRunning := result;
lbProcesses.Items.AddStrings(SL);
lbProcesses.Items.Append('Server started');
...
end;

变量binding并不是idTCPserver的一部分。那么这
Binding := IdTCPServer.Bindings.Add;
Binding.IP := lbIPs.Items.Strings;
Binding.Port := StrToInt(edtPort.Text);
就不能把IP和port的值赋给idTCPServer啊?那这段话有什么用处?

Binding := IdTCPServer.Bindings.Add;
这条语句就是增加一条bindings然后赋给binding变量,可是这和激活idTCPServer有什么联系呢?IP地址和端口Port是赋给binding变量的,并没有赋给idTCPServer啊!!

难道binding和IdTCPServer.bindings有什么内在联系吗?
是不是Binding := IdTCPServer.Bindings.Add;后,Binding变量和IdTCPServer.Bindings就是同一个东西了?
 
TIdSocketHandle encapsulates the low-level socket binding and its associated functions. A socket binding is the handle used for sending and receiving data, making and closing a connection, or waiting for a connection (listening).
TIdSocketHandle is used by TIdTCPConnection descendants in Indy to encapsulate the low-level connection and methods used to access the protocol stack in Binding.
 
Bindings是从TOwnerCollections继承的,它的Add对象,返回TCollectionItem子类的对象,所以Binding这个实际上是Bindings的Item类对象,类似于TTreeView和TTreeNode/TListView和TListItem这样的关系.

Bindings实际上"绑定"的意思,TCPServer必须要绑定几个IP和相关的端口才能监听,所以这里就是要给出绑定的IP和端口的意思.详细的东西参阅Indy的Help和Document文档,都有详细解释.
 
多人接受答案了。
 
后退
顶部