LAN Messager


Delphi7 Delhi 7 Application

Membangun aplikasi Local Area Network Messager dengan Dekphi 7

uses … Sockets ;

type

TForm1 = class(TForm)

TcpServer1: TTcpServer;

TcpClient1: TTcpClient;

Memo1: TMemo;

Memo2: TMemo;

Edit1: TEdit;

ComboBox1: TComboBox;

RadioGroup1: TRadioGroup;

Button2: TButton;

Button3: TButton;

procedure FormCreate(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure TcpServer1Accept(sender: TObject; ClientSocket: TCustomIpClient);

private

public

end;

TClientDataThread = class(TThread)

private

public

ListBuffer :TStringList;

TargetList :TStrings;

constructor Create(CreateSuspended: Boolean);

procedure synchAddDataToControl;

procedure Execute; override;

procedure Terminate;

end;

Var

Form1: TForm1;

implementation

{$R *.dfm}

constructor TClientDataThread.Create(CreateSuspended: Boolean);

begin

inherited Create(CreateSuspended);

FreeOnTerminate := true;

ListBuffer := TStringList.Create;

end;

procedure TClientDataThread.synchAddDataToControl;

begin

TargetList.AddStrings(ListBuffer);

end;

procedure TClientDataThread.Execute;

begin

Synchronize(synchAddDataToControl);

end;

procedure TClientDataThread.Terminate;

begin

ListBuffer.Free;

inherited;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

TcpClient1.RemotePort := ‘1601’ ;

TcpServer1.LocalPort := ‘1601’ ;

Button2.Caption := ‘ON’;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if Button2.Caption = ‘ON’ then begin

TcpServer1.Active := True;

Button2.Caption := ‘OFF’;

Exit;

end else begin

TcpServer1.Active := False;

Button2.Caption := ‘ON’;

Exit;

end;

end;

procedure TForm1.TcpServer1Accept(sender: TObject; ClientSocket: TCustomIpClient);

var

KompNama, s: string;

Data1: TClientDataThread;

begin

Data1:= TClientDataThread.Create(true);

Data1.TargetList := Memo1.lines;

KompNama:= ClientSocket.LookupHostName(ClientSocket.RemoteHost);

Data1.ListBuffer.Add(KompNama +’ :’ );

s := ClientSocket.Receiveln;

while s <> ” do begin

Data1.ListBuffer.Add(s);

s := ClientSocket.Receiveln;

end;

Data1.Resume;

end;

procedure TForm1.Button3Click(Sender: TObject);

var

I, J: Integer;

begin

If RadioGroup1.ItemIndex = 0 then begin

for J:=0 to ComboBox1.Items.Count-1 do begin

TcpClient1.RemoteHost := ComboBox1.Items[J];

Try

if TcpClient1.Connect then

if StringReplace(Edit1.Text, ‘ ‘, ”, [rfReplaceAll, rfIgnoreCase]) <> ” then TcpClient1.Sendln(‘ <= ‘+Edit1.Text+’ => ‘);

for I := 0 to Memo2.Lines.Count – 1 do TcpClient1.Sendln(Memo2.Lines[I]);

finally

TcpClient1.Disconnect;

end;

end;

end else begin

if ComboBox1.Text=” then begin

ShowMessage(‘Pilih PC Tujuan …!’);

Exit;

end;

TcpClient1.RemoteHost := ComboBox1.Text;

try

if TcpClient1.Connect then

if StringReplace(Edit1.Text, ‘ ‘, ”, [rfReplaceAll, rfIgnoreCase]) <> ” then TcpClient1.Sendln(‘ <= ‘+Edit1.Text+’ => ‘);

for I := 0 to Memo2.Lines.Count – 1 do TcpClient1.Sendln(Memo2.Lines[I]);

finally

TcpClient1.Disconnect;

end;

end;

Memo1.Lines.Add(‘Saya => ‘ );

for I := 0 to Memo2.Lines.Count – 1 do Memo1.Lines.Add(Memo2.Lines[i]);

Memo2.Lines.Clear ;

end;

Download bin file Disini ini.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s