Einzelnen Beitrag anzeigen

HugoHase

Registriert seit: 4. Mai 2008
Ort: Berlin
30 Beiträge
 
Delphi 7 Enterprise
 
#1

TClientSocket / TSeverSocket --- Gruppenchat

  Alt 4. Mai 2008, 21:17
Also ich Programmiere zur zeit einen Gruppenchat...
es functioniert auch alles ganz gut bis sich einer abmeldet ...
dazu könnt ihr ja mal ne Blick auf den Quellcode werfen:
(ich würde mich über hilfe freuen ... sehr sogar Thx im vorraus)

Server:
Delphi-Quellcode:
unit GUI;

interface

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

type
  TLog=record
   name : string[20];
   hash : string[4];
  end;

  TMember=record
   name : string[20];
   CO : Integer;
  end;

  AMember = array of TMember;
  TLogFile = File of TLog;

  TFenster = class(TForm)
    MemoVerlauf: TMemo;
    ListBoxKontakte: TListBox;
    ButtonKick: TButton;
    ServerSocket: TServerSocket;
    procedure vON(text:string);
    procedure vAN(text:string);
    procedure vNE(text:string);
    procedure vLO(text:string);
    procedure vTE(text:string);
    procedure FormCreate(Sender: TObject);
    procedure ServerSocketClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocketClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocketClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ButtonKickClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Fenster: TFenster;
  Member:AMember;

implementation

{$R *.dfm}

procedure TFenster.FormCreate(Sender: TObject);
begin
 setlength(member,0);
end;

procedure TFenster.ServerSocketClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var i: integer;
begin
 sleep(500);
 SetLength(Member,length(Member)+1);
 Member[Length(member)-1].name:='Member'+IntToStr(length(Member)-1);
 Member[Length(member)-1].CO:=length(Member)-1;
 ListBoxKontakte.Clear;
 for i:=0 to length(Member)-1 do
 begin
  ListBoxKontakte.Items.Add(Member[i].name);
 end;
 ServerSocket.Socket.Connections[personen].SendText('CO'+IntToStr(length(Member)-1));
end;

procedure TFenster.ServerSocketClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin

end;

procedure TFenster.ServerSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var merke,kopf,text:string;
begin
 merke:=Socket.ReceiveText;

 Kopf:=merke[1]+merke[2];
 Text:=copy(merke,3,length(merke)-2);

 if Kopf ='ANthen
  vAN(text);

 if Kopf = 'NEthen
  vNE(text);

 if Kopf = 'ONthen
  vON(text);

 if Kopf = 'LOthen
   vLO(text);

 if Kopf = 'TEthen
   vTE(text);
end;

procedure TFenster.vAN(text:string);
var ok : boolean;
    i, CO : integer;
    LogFile : TLogFile;
    mLog,Log : TLog;
begin
 Log.name:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 Log.hash:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 CO:=StrToInt(text);
 assignfile(LogFile,'LogFile.log');
 try
  reset(LogFile);
 except
 end;
 ok:=false;
 for i:=0 to FileSize(LogFile)-1 do
 begin
  Read(LogFile,mLog);
  if (Log.name=mLog.name)and(Log.hash=mLog.hash) then
   ok:=true;
 end;
 if ok then
 begin
  ServerSocket.Socket.Connections[CO].SendText('PWOK');
  MemoVerlauf.Lines.Add('*** Come in and find out '+Log.name+' ***');
  for i:=0 to length(Member)-1 do
  begin
   ServerSocket.Socket.Connections[i].SendText('TE'+'*** Come in and find out '+Log.name+' ***');
  end;
 end
 else
  ServerSocket.Socket.Connections[CO].SendText('PWKO');
 closeFile(LogFile);
end;

procedure TFenster.vNE(text:string);
var LogFile : TLogFile;
    mLog, Log : TLog;
    i, CO : integer;
    ok : boolean;
begin
 Log.name:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 Log.hash:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 CO:=StrToInt(text);
 assignfile(LogFile,'LogFile.log');
 try
  reset(LogFile);
 except
  rewrite(LogFile);
 end;
 ok:=true;
 for i:=0 to FileSize(LogFile)-1 do
 begin
  Read(LogFile,mLog);
  if mLog.name=Log.name then
   ok:=false;
 end;
 if ok then
 begin
  Write(LogFile,Log);
  ServerSocket.Socket.Connections[CO].SendText('PWOK');
  MemoVerlauf.Lines.Add('*** Come in and find out '+Log.name+' ***');
  for i:=0 to length(Member)-1 do
  begin
   ServerSocket.Socket.Connections[i].SendText('TE'+'*** Come in and find out '+Log.name+' ***');
  end;
 end
 else
 begin
  ServerSocket.Socket.Connections[CO].SendText('PWKO');
 end;
 closeFile(LogFile);
end;

procedure TFenster.vLO(text:string);
var Liste, name : String;
    CO, i : integer;
begin
 sleep(100);
 CO:=StrToInt(text);
 name:=Member[CO].name;
 for i:=CO to length(Member)-2 do
 begin
  Member[i].name:=Member[i+1].name;
  ServerSocket.Socket.Connections[Member[i].CO].SendText('CO'+IntToStr(Member[i].CO));
 end;
 SetLength(Member,length(Member)-1);
 ListBoxKontakte.Clear;
 Liste:='';
 for i:=0 to length(Member)-1 do
 begin
  Liste:=Liste+Member[i].name+'|';
  ListBoxKontakte.Items.Add(Member[i].name);
 end;
 for i:=0 to length(Member)-1 do
  ServerSocket.Socket.Connections[i].SendText('LI'+Liste);
 MemoVerlauf.Lines.Add('*** We will miss you '+name+' ***');
 for i:=0 to length(Member)-1 do
  ServerSocket.Socket.Connections[i].SendText('TE'+'*** We will miss you '+name+' ***');
end;

procedure TFenster.vON(text:string);
var name, Liste : String;
    CO, i : integer;
begin
 name:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 CO:=StrToInt(text);
 ListBoxKontakte.Clear;
 Liste:='';
 for i:=0 to length(Member)-1 do
 begin
  if CO=Member[i].CO then
   Member[i].name:=name;
  ListBoxKontakte.Items.Add(Member[i].name);
  Liste:=Liste+Member[i].name+'|';
 end;
 for i:=0 to length(Member)-1 do
 begin
  ServerSocket.Socket.Connections[i].SendText('LI'+Liste);
 end;
end;

procedure TFenster.vTE(text:string);
var nachricht, name : string;
    mCO, i : integer;
begin
 nachricht:=copy(text,1,pos('|',text)-1);
 delete(text,1,pos('|',text));
 mCO:=StrToInt(text);
 i:=-1;
 repeat
  inc(i);
 until Member[i].CO = mCO;
 name:=Member[i].name;
 MemoVerlauf.Lines.Add(name+' says: '+ nachricht);
 for i:=0 to length(Member)-1 do
 begin
  ServerSocket.Socket.Connections[i].SendText('TE'+name+' says: '+ nachricht);
 end;
end;

procedure TFenster.ButtonKickClick(Sender: TObject);
var i :integer;
    name : string;
begin
 name:=ListBoxKontakte.Items.Strings[ListBoxKontakte.ItemIndex];
 for i:=0 to length(Member)-1 do
 begin
  if name = Member[i].name then
   ServerSocket.Socket.Connections[Member[i].CO].SendText('LO');
 end;
end;

end.

Client:
Delphi-Quellcode:
unit GUI;

interface

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

type
  TFenster = class(TForm)
    TextAus: TMemo;
    TextEin: TMemo;
    ListBoxOn: TListBox;
    PanelAn: TPanel;
    Status: TMemo;
    LabeledEditName: TLabeledEdit;
    LabeledEditPW: TLabeledEdit;
    ButtonOn: TButton;
    ClientSocket: TClientSocket;
    CheckBoxNew: TCheckBox;
    Timer: TTimer;
    PanelSever: TPanel;
    ComboBoxServer: TComboBox;
    ButtonStart: TButton;
    CheckBoxFH: TCheckBox;
    procedure ShowApplication;
    procedure vCO(text:string);
    procedure vPW(text:string);
    procedure vLI(text:string);
    procedure vTE(text:string);
    procedure ButtonOnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClientSocketDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure TimerTimer(Sender: TObject);
    procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure LabeledEditNameKeyPress(Sender: TObject; var Key: Char);
    procedure LabeledEditPWKeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TextEinKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonStartClick(Sender: TObject);
    procedure TextAusChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Fenster: TFenster;
  NName:String;
  CO:String;
  pw:string;
implementation

{$R *.dfm}

procedure TFenster.ButtonOnClick(Sender: TObject);
var hash:String;
begin
 if (LabeledEditName.Text<>'') and (pw<>'') then
 begin
  hash:=uHash.hash(PW,19,2141);
  pw:='';
  if CheckBoxNew.Checked then
   ClientSocket.Socket.SendText('NE'+LabeledEditName.Text+'|'+hash+'|'+CO)
  else
   ClientSocket.Socket.SendText('AN'+LabeledEditName.Text+'|'+hash+'|'+CO);
  LabeledEditPW.Text:='';
 end
 else
  showmessage('Type in name and password!');
end;

procedure TFenster.FormCreate(Sender: TObject);
begin
 randomize;
 pw:='';
end;

procedure TFenster.ClientSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 ClientSocket.Socket.SendText('LO'+CO);
 ClientSocket.Active:=false;
 close;
 SHowmessage('You are offline!'+#10+#13+'Server is gone!');
end;

procedure TFenster.ClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 Status.Lines.Add('Welcome, please type in your name and password!');
 ButtonOn.Enabled:=true;
end;

procedure TFenster.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var merke,Kopf, Text:String;
begin
 merke:=Socket.ReceiveText;

 Kopf:=merke[1]+merke[2];
 Text:=copy(merke,3,length(merke)-2);

 if Kopf ='PWthen
  vPW(text);

 if Kopf = 'COthen
  vCO(text);

 if Kopf = 'LIthen
  vLI(text);

 if Kopf = 'TEthen
  vTE(text);

 if Kopf = 'LOthen
  close;

end;

procedure TFenster.vPW(text:string);
begin
 if text='OKthen
 begin
  NName:=LabeledEditName.Text;
  ClientSocket.Socket.SendText('ON'+NName+'|'+CO);
  PanelAn.Hide;
  TextEin.SetFocus;
 end
 else
 begin
  Status.Lines.Add('Invalid password');
  Status.Lines.Add('Access denided');
  LabeledEditPW.Text:='';
  pw:='';
 end;
end;

procedure TFenster.vLI(text:string);
var na:string;
begin
 ListBoxOn.Clear;
 while length(text)>0 do
 begin
  na:=copy(text,1,pos('|',text)-1);
  delete(text,1,pos('|',text));
  ListBoxOn.Items.Add(na);
 end;
end;

procedure TFenster.vCO(text:string);
begin
 CO:=text;
end;

procedure TFenster.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 if alphablendvalue<247 then
  AlphaBlendValue:=AlphaBlendValue+10
 else
  alphablendvalue:=255;
end;

procedure TFenster.TimerTimer(Sender: TObject);
begin
 if not Fenster.Active then
  if AlphaBlendValue>10 then
   AlphaBlendValue:=AlphaBlendValue-2
 else
  if alphablendvalue<247 then
   AlphaBlendValue:=AlphaBlendValue+10
  else
   alphablendvalue:=255;
end;

procedure TFenster.ClientSocketError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
 case ErrorCode of
  10061 : begin
           ErrorCode:=0;
           showmessage('Sever not found!'+#10+#13+'Please, try again later!');
           close;
          end;
 end;
end;

procedure TFenster.LabeledEditNameKeyPress(Sender: TObject; var Key: Char);
begin
 case key of
 char(vk_return) : begin
                    key:=#0;
                    LabeledEditPW.SetFocus;
                   end;
 '|'             : key:=#0;
 else

 end;
end;

procedure TFenster.LabeledEditPWKeyPress(Sender: TObject; var Key: Char);
var i, anzahl : integer;
begin
 anzahl:=random(3);
 for i:=0 to anzahl do
  LabeledEditPW.Text:=LabeledEditPW.Text+'*';
 case key of
  chr(vk_return) : begin
                    ButtonOnClick(Sender);
                    key:=#0;
                   end;
  chr(vk_back) : begin
                    LabeledEditPW.Text:='';
                    pw:='';
                    key:=#0;
                   end;

 else
  pw:=pw+key;
  key:=#0;
 end;
end;

procedure TFenster.FormClose(Sender: TObject; var Action: TCloseAction);
var FileName, m1,m2,m3 : string;
begin
 FileName:='D'+DateToStr(date)+' T';
 m1:=timetostr(time);
 m2:=copy(m1,1,pos(':',m1)-1);
 delete(m1,1,pos(':',m1));
 m2:=m2+'-';
 m3:=copy(m1,1,pos(':',m1)-1);
 delete(m1,1,pos(':',m1));
 m2:=m2+m3+'-'+m1;
 FileNAme:=FileName+m2;
 if DirectoryExists('Verlauf') then
 begin
  TextAus.Lines.SaveToFile('Verlauf\'+FileName+'.vlf');
 end
 else
 begin
  MkDir('Verlauf');
  TextAus.Lines.SaveToFile('Verlauf\'+FileName+'.vlf');
 end;
 ClientSocket.Active:=false;
end;

procedure TFenster.TextEinKeyPress(Sender: TObject; var Key: Char);
var text : string;
begin
 if key = chr(13) then
 begin
  key:=#0;
  text:=TextEin.Lines.Text;
  TextEin.Clear;
  if lowercase(text) = 'exitthen
   close;
  ClientSocket.Socket.SendText('TE'+text+'|'+CO);
 end;
end;

procedure TFenster.vTE(text:string);
begin
 TextAus.Lines.Add(text);
end;

procedure TFenster.ButtonStartClick(Sender: TObject);
begin
 ClientSocket.Host:=ComboBoxServer.Text;
 try
  ClientSocket.Active:=true;
 except

 end;
 PanelSever.Visible:=false;
end;

end.
I hacked 127.0.0.1
  Mit Zitat antworten Zitat