2024年3月27日发(作者:)

IdTcpServer/IdTcpClient

IdTcpServer

uses IdContext //需要引用

属性,方法:

:=True; //开启服务器

:= '127.0.0.1';//绑定IP

:= 7956;//绑定端口

事件:

OnConnect : 客户端连接成功触发

OnDisConnect : 客户端断开触发

OnExeCute : 收到客户端数据触发

例子

//像所有客户断发送数据

var

I: Integer;

Context: TIdContext;

begin

with st do

try

for I := 0 to Count - 1 do

begin

Context := TIdContext(Items[I]);

('Hello,everybody!');

end;

finally

List;

end;

end;

//向某个客户发送数据

var

I: Integer;

Context: TIdContext;

begin

with st do

try

for I := 0 to Count - 1 do

begin

Context := TIdContext(Items[I]);

if <> '192.168.10.90' then

continue;

('Hello!');

end;

finally

List;

end;

end;

IdTcpClient

属性,方法:

ConnectTimeOut:连接超时

Host:主机IP或域名

IPVersion:Ip版本 ipv4 or ipv6

Name:控件名

Port:主机端口

ReadTimeOut:读取超时

t; //连接服务端

nect;//端开连接

ted;//是否连接成功 返回true 连接成功

n('aa');// 向服务端发送数据

OnWork事件AWorkMode=wmRead 表示有收到数据