本人看了Delphi书里的网络编程,可是书上给的例子是TCPClientSocket和TCPServerSocket控件,不过我想使用TIdTCPClient和TIdTCPServer本来是想用控制台写的.不过对线程不太熟悉.麻烦高人给一个详细的例子TIdTCPClient和TIdTCPServer连接到ListView里.最好是使用多线程的.谢谢了!

解决方案 »

  1.   

    一个简单的indytcp发送和接收的例子unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
      StdCtrls, Buttons, IdTCPServer;type
      Tuser = packed record
        s_id:string[255];
        s_name:string[255];
        end;
      TForm1 = class(TForm)
        BitBtn1: TBitBtn;
        IdTCPClient1: TIdTCPClient;
        IdTCPServer1: TIdTCPServer;
        Memo1: TMemo;
        procedure BitBtn1Click(Sender: TObject);
        procedure IdTCPServer1Execute(AThread: TIdPeerThread);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      user:Tuser;
    begin
      user.s_id:='21212121';
      self.IdTCPClient1.Host:='127.0.0.1';
      self.IdTCPClient1.Port:=9999;
      try
        self.IdTCPClient1.Connect(5000);
        self.IdTCPClient1.WriteBuffer(user,sizeof(Tuser));
        self.IdTCPClient1.Disconnect;
      except
        on e:exception do
          memo1.Lines.Add(e.Message);
      end;
    end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
      user: TUser;
    begin
      with AThread.Connection do
      begin
        ReadBuffer(user,sizeof(TUser));
        memo1.Lines.Add(user.s_id);//这里处理添加到listview?
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
    self.IdTCPServer1.DefaultPort:=9999;
    self.IdTCPServer1.Active:= true;
    end;end.
      

  2.   

    请 LS 看清楚题目,人家是要求链接到 LISTVIEW 里,继续观望期待大牛出现.
      

  3.   

    你怎样理解这个"链接到 LISTVIEW 里"?
      

  4.   


    你的问题本身就莫名其妙,TIdTCPClient和TIdTCPServer连接到LISTVIEW什么意思?
    难道把LISTVIEW挖个洞,把那两个控件镶嵌进去?
    估计就是把接收的数据放入LISTVIEW中嘛,月亮的程序除了没有写LISTVIEW外,别的都有了。
    我也期望还有比月亮更牛的大牛出现在你的帖子里,观望中。
      

  5.   

    是不是用LISTVIEW来显示连接到服务器上的用户?
    http://2ccc.com/article.asp?articleid=2032
      

  6.   

    俺有程序,可以二次开发,免费版本仅支持5 user,哈哈
      

  7.   

    月亮,连接的问题我写好了,服务端是控制台写的,现在问题是.我想在服务端里添加一个读取www.xxx.com/ip.txt里的IP地址.然后返回给客户端.
      

  8.   

    在服务端用下载函数把那个TXT下载下来,然后读去IP,然后再转发给客端就可以 
      

  9.   

    鹏哥,提问题我的标题虽然有点弱弱的,但是只要能解决我的问题,我只能厚着脸皮发主题了。呵呵。哎没办法,自己切实是太菜了。控制台写Server60%都是参考其它远控。刚才sanguomi给了我一个很好的意见,嗯,一定要试试。