unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var  Server:string;
begin
If ClientSocket1.Active = False Then
Begin
If InputQuery('要連線到那臺電腦', 'IP', Server) then
If Length(Server) > 0 then
with ClientSocket1 do
begin
Host := Server;
Active := True;
end;
Button1.Caption := '關閉' ;
End
else begin
ClientSocket1.Active := False ;
Button1.Caption := '連線' ;
End
end;

procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
begin
Edit1.Text:=(Socket.ReceiveText);
Edit2.Text:=(Socket.ReceiveText);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If ClientSocket1.Active = True then ClientSocket1.Close;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
If ClientSocket1.Active = True then ClientSocket1.Close;
end;

end.
