unit Chat; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdTCPServer, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient; type TForm1 = class(TForm) Label1: TLabel; AdresseCible: TEdit; Label2: TLabel; PortCible: TEdit; Connecter: TButton; Deconnecter: TButton; Label4: TLabel; Message: TEdit; Chat: TMemo; IdTCPClient1: TIdTCPClient; IdTCPServer1: TIdTCPServer; Label3: TLabel; Pseudo: TEdit; procedure ConnecterClick(Sender: TObject); procedure DeconnecterClick(Sender: TObject); procedure MessageKeyPress(Sender: TObject; var Key: Char); procedure IdTCPServer1Connect(AThread: TIdPeerThread); procedure IdTCPServer1Execute(AThread: TIdPeerThread); private { Déclarations privées } public { Déclarations publiques } end; TReceptionThread = class(TThread) protected procedure Execute; override; end; var Form1: TForm1; ReceptionThread : TReceptionThread; implementation {$R *.dfm} procedure TReceptionThread.Execute; var s : String; begin while not Terminated do begin s:=''; try s:=Form1.IdTCPClient1.ReadLn; except Terminate; end; Form1.Chat.Lines.Add(S); end; end; procedure TForm1.ConnecterClick(Sender: TObject); begin if (Form1.pseudo.Text='') then ShowMessage('Vous devez entrer un pseudo...') else with IdTCPClient1 do begin Host:=AdresseCible.Text; Port:=StrToInt(PortCible.Text); Connect; Connecter.Enabled:=False; ReceptionThread:=TReceptionThread.Create(False); end; end; procedure TForm1.DeconnecterClick(Sender: TObject); begin idTCPClient1.Disconnect; Connecter.Enabled:=True; end; procedure TForm1.MessageKeyPress(Sender: TObject; var Key: Char); begin if (key=#13) then begin IdTCPClient1.WriteLn(Pseudo.Text + '> ' + Message.Text); Form1.Chat.Lines.Add(Pseudo.Text + '> ' + Message.Text); Message.Text:=''; end; end; procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread); begin with AThread.Connection do begin Writeln('Connexion établie'); end; end; procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread); var s : String; begin While Athread.Connection.Connected do begin s:=AThread.Connection.Readln; Form1.Chat.Lines.Add(S); end; end; end.