我用Delphi里的TCPServer/TCPClient控件编写了一个简单的发送信息的程序,服务器端负责接收并且显示客户端发送过来的数据,而客户端只负责发送任务,但是有个问题,只有第一次发送信息之后,服务器端才会显示接收到的信息,如果再发就不显示了。
下面是服务器端的代码:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,IdStack, StdCtrls, Sockets;type
  TForm1 = class(TForm)
    TcpServer1: TTcpServer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure TcpServer1Accept(Sender: TObject;
      ClientSocket: TCustomIpClient);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  Label3.Caption := TcpServer1.LocalHost;
  Label4.Caption := TcpServer1.LocalPort;
  TcpServer1.Active := True;
  Memo1.Clear; 
end;procedure TForm1.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  s : String;
begin
  s := ClientSocket.Receiveln();
  Memo1.Lines.Add(s);
end;end.下面是客户端程序:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Sockets;type
  TForm1 = class(TForm)
    TcpClient1: TTcpClient;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  Label3.Caption := TcpClient1.RemoteHost;
  Label4.Caption := TcpClient1.RemotePort;
  TcpClient1.Active := True;
  Memo1.Clear;
end;//发送信息
procedure TForm1.Button1Click(Sender: TObject);
var
  s : String;
  r : String;
begin
  s := Edit1.Text;
  TcpClient1.Sendln(s);
  r := TcpClient1.Receiveln();
  Memo1.Lines.Add(r);
end;end.
请各位大侠帮我这个Delphi初学者解决一下,拜托!

解决方案 »

  1.   

    断开再连接试试看:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      s : String;
    begin
      TcpClient1.Active:= true;
      s := Edit1.Text;
      TcpClient1.Sendln(s);
      TcpClient1.Active:= false;
    end;
      

  2.   

    TCPServer的非阻塞式编程需要使用的线程技术的,象你这样的代码只会在Client断开连接时Server才会收到数据。
      

  3.   

    建议换成TServerSocket/TClientSocket组件,它们都提供了数据到达的事件,适合初学TCP/IP通讯编程。在Delphi7中,默认是没有TServerSocket/TClientSocket组件的,需要手工添加。添加的方法如下:选择Component菜单->Install Packages选择Add...按钮选择C:\Program Files\Borland\Delphi7\Bin\dclsockets70.bpl完成安装。
      

  4.   

    要求是局域网中的TCP/IP通信,所以没有办法只有用这两个控件!
      

  5.   

    TServerSocket/TClientSocket也是完成TCP/IP通讯的,它们和TCPServer/TCPClient一样,都是Delphi组件,而不是ActiveX控件,所以都不需要在运行的电脑上安装什么控件的。
      

  6.   

    可以用TIdTCPClient和TIdTCPServer组件