Einzelnen Beitrag anzeigen

RedShakal
(Gast)

n/a Beiträge
 
#1

Konsolen TCP + Socketserver

  Alt 8. Jan 2010, 20:33
Hallo, ich wollte gerade meinem Konsolen TCP Server noch einen SocketChat hinzufügen. Der Server wird initialisiert, jedoch werden die Ereignisse nicht aufgerufen. Ich wollte euch Fragen ob ihr mal über meinen Source schauen könnt wo der Fehler liegen könnte. Der TCP Server funktioniert im übrigen.

Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

uses
 SysUtils, IdTCPServer, IdContext, IdBaseComponent, IdComponent, IniFiles, Classes, ScktComp;
type
 TServer = class(TObject)
   IdTCPServer1: TIdTCPServer;
   procedure IdTCPServer1Execute(AThread: TIdContext);
   procedure IdTCPServer1Connect(AThread: TIdContext);
 private
   { Private declarations }
   constructor Create;
 public
   { Public declarations }
 end;

 TChatServer = class(TObject)
    Chat: TServerSocket;
    Port: Word;
    constructor Create;
    procedure OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure OnClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure OnClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
  end;

 Const
   Serverver = '0.03';

type
  TDynStringArray = Array of string;

var
  sl : TStringList;
  Zeile : string;
  Clientver : string;
  i : Integer;

function Explode(const Separator, S :String; Limit :Integer = 0): TDynStringArray;
  var
    SepLen: Integer;
    F, P: PChar;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = 'then
    begin
      SetLength(Result, 1);
      Result[0] := S;
      Exit;
    end;
  SepLen := Length(Separator);

  P := PChar(S);
  while P^ <> #0 do
    begin
      F := P;
      P := AnsiStrPos(P, PChar(Separator));
      if (P = nil) or ((Limit > 0) and (Length(Result) = Limit - 1)) then
        P := StrEnd(F);
      SetLength(Result, Length(Result) + 1);
      SetString(Result[High(Result)], F, P - F);
      F := P;
      if P = Separator then
        SetLength(Result, Length(Result) + 1);
      while (P^ <> #0) and (P - F < SepLen) do
        Inc(P);
    end;
end;


// ---------------------------- Chatserver ---------------------------- \\


procedure TChatServer.OnClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  writeln(Socket.RemoteAddress+' hat sich im Chatserver eingeloggt.');
end;

procedure TChatServer.OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  MSG : String;
begin
  MSG := Socket.RemoteAddress+' : '+Socket.ReceiveText;
  writeln(MSG);
  for i := 0 to Chat.Socket.ActiveConnections-1 do
    Chat.Socket.Connections[i].SendText(MSG);
end;

procedure TChatServer.OnClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  writeln(Socket.RemoteAddress+' hat sich vom Chatserver ausgeloggt.');
end;

// ---------------------------- Loginserver ---------------------------- \\

procedure TServer.IdTCPServer1Execute(AThread: TIdContext);
var
  Data : String;
  StrArr : TDynStringArray;
  ini : TIniFile;
   f : TextFile;
   Files : string;
   y,m,d : Word;
   Admin : Integer;
begin
with AThread.Connection do
   begin
     DecodeDate(Now,d,m,y);
     Files := Format('log\%.2d-%.2d-%.2d.txt',[y,m,d]);

     //AssignFile(f, Files);

     //if FileExists(Files) then Append(f) else Rewrite(f);

     Data := String(Socket.ReadLn);
     Data := Trim(Data);

     if Length(Data) > 0 then
       begin
         StrArr := Explode('|', Data);
         if StrArr[0] = 'loginthen
           begin
             AssignFile(f,'version.txt');
             Reset(f);
             If IoResult=0 then
               begin
                ReadLn(f, Clientver);
                CloseFile(f);
               end;
               if StrArr[3] = Clientver then
                 begin
                   if FileExists('accounts\'+StrArr[1]+'.ini') then
                     begin
                       ini := TIniFile.Create('accounts\'+StrArr[1]+'.ini');
                       try
                         if StrArr[2] = ini.ReadString('Login','Passwort','') then
                           begin
                             Admin := ini.ReadInteger('Admin','Level',0);
                             if Admin = 0 then
                               begin
                                 Socket.WriteLn('login');
                                 WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' hat sich eingeloggt.');
                                //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' hat sich eingeloggt.');
                               end;
                             if Admin = 1 then
                               begin
                                 Socket.WriteLn('login|admin');
                                 WriteLn('['+TimeToStr(Time)+']'+' Admin: '+StrArr[1]+' hat sich eingeloggt.');
                                //WriteLn(f, '['+TimeToStr(Time)+']'+' Admin: '+StrArr[1]+' hat sich eingeloggt.');
                               end;
                           end
                         else
                           begin
                             Socket.WriteLn('invalid');
                             WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' falsches Passwort.');
                             //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' falsches Passwort.');
                           end;
                       finally
                         ini.free;
                       end;
                     end
                   else
                     begin
                       Socket.WriteLn('na');
                       WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' existiert nicht.');
                       //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' existiert nicht.');
                     end;
                 end
                   else
                     begin
                       Socket.WriteLn('version');
                     end;
           end;

         if StrArr[0] = 'registerthen
           begin
             if FileExists('accounts\'+StrArr[1]+'.ini') then
               begin
                 Socket.WriteLn('vorhanden');
                 WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' existiert bereits.');
                 //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' existiert bereits.');
               end
             else
               begin
                 try
                   ini := TIniFile.Create('accounts\'+StrArr[1]+'.ini');
                   ini.WriteString('Login','Passwort',StrArr[2]);
                   ini.WriteString('Login','E-Mail',StrArr[3]);
                   ini.WriteString('Data','Geschlecht',StrArr[4]);
                   ini.WriteString('Data','Geburtstag',StrArr[5]);
                   ini.WriteString('Data','ICQ',StrArr[6]);
                   ini.WriteString('Data','Name',StrArr[7]);
                   ini.WriteInteger('Stats','gespielt',0);
                   ini.WriteInteger('Stats','gewonnen',0);
                   ini.WriteInteger('Stats','verloren',0);
                   ini.WriteString('Stats','Spielzeit','00:00:00');
                   ini.WriteInteger('Admin','Level',0);
                   ini.WriteInteger('Admin','PermBan',0);
                   ini.WriteString('Admin','TempBan','1.1.2009');
                   ini.WriteString('Admin','Grund','');
                   ini.WriteString('Info','MAC','');
                 finally
                   Socket.WriteLn('erfolg');
                   ini.free;
                   WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' wurde registriert.');
                   //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' wurde registriert.');
                 end;
               end;
           end;
         if StrArr[0] = 'changepwthen
           begin
             if FileExists('accounts\'+StrArr[1]+'.ini') then
               begin
                 ini := TIniFile.Create('accounts\'+StrArr[1]+'.ini');
                   if StrArr[2] = ini.ReadString('Login','Passwort','') then
                     begin
                       ini.WriteString('Login','Passwort',StrArr[3]);
                       Socket.WriteLn('pwchange');
                       WriteLn('['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' hat sein Passwort geaendert.');
                     //WriteLn(f, '['+TimeToStr(Time)+']'+' Account: '+StrArr[1]+' hat sein Passwort geaendert.');
                     end
                  else Socket.WriteLn('pwfalsch');
               end;
           end;
         if StrArr[0] = 'AdminInfothen
           begin
             if FileExists('accounts\'+StrArr[1]+'.ini') then
               begin
                 ini := TIniFile.Create('accounts\'+StrArr[1]+'.ini');
                 try
                   Admin := ini.ReadInteger('Admin','Level',0);
                   if (StrArr[2] = ini.ReadString('Login','Passwort','')) and (Admin = 1) then
                     begin
                       if FileExists('accounts\'+StrArr[3]+'.ini') then
                         begin
                           ini.Free;
                           ini := TIniFile.Create('accounts\'+StrArr[3]+'.ini');
                           Socket.WriteLn('AdminInfo'+'|'+ini.ReadString('Login','E-Mail','')+'|'+ini.ReadString('Data','Geschlecht','')+'|'+ini.ReadString('Data','Geburtstag','')+'|'+ini.ReadString('Data','ICQ','')+'|'+ini.ReadString('Data','Name','')+'|'+InttoStr(ini.ReadInteger('Stats','gespielt',0))+'|'+InttoStr(ini.ReadInteger('Stats','gewonnen',0))+'|'+InttoStr(ini.ReadInteger('Stats','verloren',0))+'|'+ini.ReadString('Stats','Spielzeit','')+'|'+InttoStr(ini.ReadInteger('Admin','Level',0))+'|'+InttoStr(ini.ReadInteger('Admin','PermBan',0))+'|'+ini.ReadString('Admin','TempBan','')+'|'+ini.ReadString('Admin','Grund',''));
                           WriteLn('['+TimeToStr(Time)+']'+' Admin: '+StrArr[1]+' hat Informationen über '+StrArr[3]+' angefordert.');
                           //WriteLn(f, '['+TimeToStr(Time)+']'+' Admin: '+StrArr[1]+' hat Informationen über '+StrArr[3]+' angefordert.');
                         end;
                     end
                   else Socket.WriteLn('AdminInfoNA');
                 finally
                   ini.free;
                 end;
               end;
           end;
       end;
   end;
    //CloseFile(f);
end;


procedure TServer.IdTCPServer1Connect(AThread: TIdContext);
begin
  //
end;

constructor TServer.Create;
begin
  inherited Create;
  WriteLn('#######################');
  WriteLn('## ##');
  WriteLn('## Masterserver '+Serverver+' ##');
  WriteLn('## ##');
  WriteLn('#######################');
  WriteLn('');
  IdTCPServer1 := TIdTCPServer.Create(nil);
  WriteLn('Server wurde initialisiert.');
  IdTCPServer1.DefaultPort := 5000;
  WriteLn('Server lauscht auf Port: '+InttoStr(IdTCPServer1.DefaultPort));
  WriteLn('');
  IdTCPServer1.TerminateWaitTime := 5000;
  IdTCPServer1.OnConnect := IdTCPServer1Connect;
  IdTCPServer1.OnExecute := IdTCPServer1Execute;
end;

constructor TChatServer.Create;
begin
  inherited Create;
  Chat := TServerSocket.Create(nil);
  WriteLn('Chatserver wurde initialisiert.');
  Chat.Port := 5002;
  WriteLn('Chatserver lauscht auf Port: '+InttoStr(Chat.Port));
  Chat.OnClientConnect := OnClientConnect;
  Chat.OnClientDisconnect := OnClientDisconnect;
  Chat.OnClientRead := OnClientRead;
end;


var
  Server : TServer;
  ChatServer : TChatServer;
begin
  Server := TServer.Create;
  Server.IdTCPServer1.Active := True;
  ChatServer := TChatServer.Create;
  ChatServer.Chat.Active := True;
  While True do Sleep(50);
end.
Ich vermute der Fehler liegt darin, das ich 2 Server nacheinander erstelle und irgentwie die Ereignisse durch das While True do Sleep(50); nicht aufrufe. Der TCP Server funktioniert Standalone im übrigen Prima.

Ich hoffe ihr könnt mir helfen. Vielleicht findet der ein oder andere sogar noch etwas, was man verbessern könnte
  Mit Zitat antworten Zitat