百度首页 | 百度空间
 
查看文章
 
IdTcpServer/IdTcpClient
2008-03-28 09:08

IdTcpServer
uses IdContext //需要引用
属性,方法:
IdTCPServer.Active :=True; //开启服务器
IdTCPServer1.Bindings.Add.IP := '127.0.0.1';//绑定IP
IdTCPServer1.Bindings.Add.Port := 7956;//绑定端口
事件:
OnConnect : 客户端连接成功触发
OnDisConnect : 客户端断开触发
OnExeCute : 收到客户端数据触发

例子
//像所有客户断发送数据
var
I: Integer;
Context: TIdContext;
begin
with IdTCPServer1.Contexts.LockList do
    try
      for I := 0 to Count - 1 do
        begin
          Context := TIdContext(Items[I]);
          Context.Connection.IOHandler.Write('Hello,everybody!');
        end;
    finally
      IdTCPServer1.Contexts.UnlockList;
    end;
end;

//向某个客户发送数据
var
I: Integer;
Context: TIdContext;
begin
with IdTCPServer1.Contexts.LockList do
    try
      for I := 0 to Count - 1 do
        begin
          Context := TIdContext(Items[I]);
          if Context.Binding.PeerIP <> '192.168.10.90' then
            continue;
          Context.Connection.IOHandler.Write('Hello!');
        end;
    finally
      IdTCPServer1.Contexts.UnlockList;
    end;
end;

IdTcpClient
属性,方法:
ConnectTimeOut:连接超时
Host:主机IP或域名
IPVersion:Ip版本 ipv4 or ipv6
Name:控件名
Port:主机端口
ReadTimeOut:读取超时
IdTCPClient1.Connect; //连接服务端
IdTCPClient1.Disconnect;//端开连接
IdTCPClient1.Connected;//是否连接成功 返回true 连接成功


IdTCPClient1.IOHandler.WriteLn('aa');// 向服务端发送数据

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


类别:delphi园地 | 添加到搜藏 | 浏览() | 评论 (2)
 
最近读者:
 
网友评论:
1
2008-04-26 09:22
好文,初学通讯,找了好久,这个最精辟。
 
2
2008-04-26 09:22
好文,初学通讯,找了好久,这个最精辟。
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu