Thema: Chat (IP)

Einzelnen Beitrag anzeigen

Schnittcher

Registriert seit: 25. Aug 2009
Ort: Blomberg
120 Beiträge
 
#10

Re: Chat (IP)

  Alt 26. Aug 2009, 08:45
Server:

Delphi-Quellcode:
unit server;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    IdIPWatch1: TIdIPWatch;
    Label1: TLabel;
    ServerSocket1: TServerSocket;
    procedure FormCreate(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
label1.Caption:= IdIPWatch1.LocalIP;
ServerSocket1.Port:= 55555;
ServerSocket1.Active:= True;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var Text:string;
i: Integer;
begin
Text:=Socket.ReceiveText;
Memo1.Lines.add(Timetostr(now)+': '+Text);
For I := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
begin
with ServerSocket1.Socket.Connections [I] do
if (Connected) then SendText(Text);
end;
end;
end.
Client:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, AppEvnts, ExtCtrls, ScktComp, IdBaseComponent, IdComponent,
  IdIPWatch;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    lbIP: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    edNachricht: TEdit;
    edIP: TEdit;
    edNickname: TEdit;
    cmdSenden: TButton;
    cmdSetzen: TButton;
    cmdNick: TButton;
    ClientSocket1: TClientSocket;
    ApplicationEvents1: TApplicationEvents;
    IdIPWatch1: TIdIPWatch;
    Timer1: TTimer;
    Label6: TLabel;
    lbNickname: TLabel;
    cmdVerlassen: TButton;
    Memo1: TMemo;
    procedure cmdNickClick(Sender: TObject);
    procedure cmdVerlassenClick(Sender: TObject);
    procedure cmdSendenClick(Sender: TObject);
    procedure cmdSetzenClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure delay(time:word);
    procedure Timer1Timer(Sender: TObject);
  private

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
 var Nickname, Nachricht:String;
{$R *.dfm}

procedure TForm1.delay(time: word);
var Start:Integer;
begin
  Start:=GetTickCount;
  while (GetTickCount) - (Start) <= time do
  Application.ProcessMessages;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
lbIP.Caption := IdIPWatch1.LocalIP; // Die aktuelle IP-Adresse wird angezeigt.
cmdSenden.Enabled:= false;
cmdSetzen.Enabled:= false;
label6.Visible:=False;
lbNickname.Visible:=False;
end;

procedure TForm1.cmdNickClick(Sender: TObject);
begin
Nickname:= edNickname.Text;
//cmdSenden.Enabled:=True;
cmdSetzen.Enabled:=True;
//cmdNick.Visible:=False;
edNickName.Enabled:=False;
Label6.Visible:=True;
//lbNickname.Visible:=True;
lbNickname.Caption:= Nickname;
Label5.Visible:=False;

end;

procedure TForm1.cmdSetzenClick(Sender: TObject);
begin
ClientSocket1.Host:= '192.168.255.123';
ClientSocket1.Port:= 55555;
ClientSocket1.Active:=True;
cmdSetzen.Enabled:= False;
edIP.Enabled:= False;
cmdSenden.Enabled:= True;
Nachricht:= edNickName.Text+' hat den Raum betreten.';
delay(500);
ClientSocket1.Socket.SendText(Nachricht);

end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
begin
Form1.Show;
Memo1.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.cmdSendenClick(Sender: TObject);
begin
if (ClientSocket1.Active=true) and (edNachricht.Text<>'') then
CLientSocket1.Socket.SendText(Nickname+': '+edNachricht.Text);
edNachricht.Text := '';
end;

procedure TForm1.cmdVerlassenClick(Sender: TObject);
begin
ClientSocket1.Socket.SendText(edNickName.Text+' hat den Raum verlassen.');
delay(500);
ClientSocket1.Close;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
lbIP.Caption:= IdIPWatch1.LocalIP;
end;

end.
  Mit Zitat antworten Zitat