unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 =
class(TForm)
btnStart: TButton;
btnStop: TButton;
btnSend: TButton;
Edit1: TEdit;
Memo1: TMemo;
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Client: TClientSocket;
Server: TServerSocket;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnStartClick(Sender: TObject);
begin
Server.Port:=44044;
Client.Port:=44044;
Client.Host:='
127.0.0.1';
//Localhost
Server.Open;
Client.Open;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
Client.Close;
Server.Close;
end;
procedure TForm1.btnSendClick(Sender: TObject);
begin
if Client.Active = true
then
begin
Client.Socket.SendText(Edit1.Text);
Edit1.Text:='
';
ServerClientRead(Sender,Client);
end;
end;
procedure TForm1.ServerClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('
***Connected.')
end;
procedure TForm1.ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('
***Disconnected.');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Client := TClientSocket.Create(self);
Server := TServerSocket.Create(self);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Client.Free;
Server.Free;
end;
end.