用TcpClient,TcpServer怎样收发数据啊,给个简单的例子就行了,
为什么下面的程序接受的数据不是TcpServer发送的数据啊
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Sockets, ScktComp;type
  aa = record
    a: integer;
    b: integer;
  end;
  TForm1 = class(TForm)
    TcpClient1: TTcpClient;
    TcpServer1: TTcpServer;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure TcpServer1Accept(Sender: TObject;
      ClientSocket: TCustomIpClient);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  a, b: aa;begin
  a.a:=10;
  a.b:=11;
  TcpClient1.Active := true;
  TcpClient1.SendBuf(a,sizeof(a));
  TcpClient1.ReceiveBuf(b,sizeof(b));
  Label1.Caption:= IntToStr(b.a);
  Label2.Caption:= IntToStr(b.b);
  TcpClient1.Active:= false;
end;procedure TForm1.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  a: aa;
begin
  clientsocket.ReceiveBuf(a,sizeof(a));
  TCPServer1.SendBuf(a,sizeof(a));
end;end.

解决方案 »

  1.   

    他们的属性值:
    Active true
    blockMode bmThreadBlocking
    LocalHost 127.0.0.1
    LocalPort 50000
    Name TcpServer1 这是TCPclient 
    active   false
    blockmode bmBlocking
    name Tcpclient1
    remotehost 127.0.0.1
    remoteport 50000
      

  2.   

    谢谢了,TcpClient怎么才能收到Tcpserver发过来的数据啊,
    Tcpserver接受到数据的时候,马上给TcpClient回应,怎么作啊,
    是不是要启动一个线程来作啊?
      

  3.   

    呵呵,
    是的,你应该在 TCPClient 边建立一个线程,procedure TTextHandleThread.Execute;
    begin
      Sleep(1);
      while not Terminated do
      begin
        if not FrmCltMain.IdTCPClient_Text.Connected then
          Terminate
        else
        try
          CmdS := FrmCltMain.IdTCPClient_Text.ReadLn(#10#10,100);
          CmdS := Trim(CmdS);
          if CmdS = 'Close' then
            FrmCltMain.TextDisConnect
          else
            CmdS := FrmCltMain.IdTCPClient_Text.ReadLn(#10#10,100);
            CmdS := Trim(CmdS);
            MsgS := FrmCltMain.IdTCPClient_Text.ReadLn(#10#10,100);
            MsgS := Trim(MsgS);
            if CmdS <> '' then
              Synchronize(HandleInput);
        except
        end;
      end;
    end;
      

  4.   

    Delphi6的C:\Program Files\Borland\Delphi6\Demos\Indy\TCPStreamClientServer下有完整的例子