Einzelnen Beitrag anzeigen

Beckman

Registriert seit: 12. Jul 2004
205 Beiträge
 
#1

console application --> events --> clientsocket prob :

  Alt 15. Aug 2004, 00:09
so, ich wollte endlich mal lernen, wie man console applications macht und haenge jetzt aber an der stelle, wo er connecten sollte...
/connect --> und dann passiert nix, er will net connecten. ich warte mit 'nc -lp 6667'
kann mir jemand erklaeren, was ich falsch mache bei der strukturierung meiner console app?
bzw wie man besser events einrichtet bei console apps...

kann den code mal jemand ausprobieren?

thanks.




Code:
program pepsi;

{$APPTYPE CONSOLE}

uses
  SysUtils, ScktComp;

type
  TEvents = class
  public
    procedure ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientConnecting(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientRead(Sender: TObject; Socket: TCustomWinSocket);
  end;

var
  Events : TEvents;
  input: String;
  Client: TClientSocket;
 
{------------------------------------------------------------------------------}

procedure connect();
begin
if Client = nil then
        begin
        Client := TClientSocket.Create(nil);
//        Client.ClientType := ctNonBlocking;
       
        Client.OnConnect := Events.ClientConnect;
        Client.OnConnecting := Events.ClientConnecting;
        Client.OnDisconnect := Events.ClientDisconnect;
        Client.OnError := Events.ClientError;
        Client.OnRead := Events.ClientRead;
        end;

Client.Host := '192.168.0.3';
Client.Port := 6667;

if Client.Active = false then
        begin
        Client.Active := true;
        end
                else
                begin
                writeln('already connected, disconnect first');
                end;
end;

procedure disconnect();
begin
if client <> nil then
        begin
        if Client.Active = true then
                begin
                try
                        begin
                        client.Active := false;
                        end;
                                except
                                writeln('cannot disconnect');
                                end;
                end
                        else
                        begin
                        writeln('not connected, connect first');
                        end;
        end
                else writeln('ahm, are u drunk?');
end;

procedure sendout(data: String);
begin
try
        begin
        Client.Socket.SendText(data +#13+#10);
        end;
                except
                writeln('cannot send to host...');
                end;
end;

function GetToken(Src: string; Index: integer; Delimiter: char): string;
var
  I: integer;
  J: integer;
  Count: integer;
  S: string;
begin
Result := '';
if Index = 0 then
        begin
        Result := Src;
        Exit;
        end

        else
        if Index < 0 then
                begin
                Index := -Index;
                J := 1;
                for I := 1 to Length(Src) do
                        begin
                        if Src[I] = Delimiter then
                                Inc(J);
                                if J >= Index then
                                        Break;
                        end;
                if J = 1 then
                        begin
                        Result := Src;
                        Exit;
                        end;
                Result := Copy(Src, I + 1, Length(Src)); // MaxInt
                Exit;
                end;
        S := Src;
        I := 0;
        Count := 1;
        while (I <= (Index - 2)) do
                begin
                J := Pos(Delimiter, S);
                if J = 0 then
                        Break;
                Delete(S, 1, J);
                Inc(I);
                end;
        for I := 1 to Length(Src) do
        if Src[I] = Delimiter then
                Inc(Count);
        if Index > Count then
                Exit;
        J := Pos(Delimiter, S);
        if J = 0 then
                begin
                J := Length(S);
                Result := Copy(S, 1, J);
                end
                        else
                        Result := Copy(S, 1, J - 1);
end;

{------------------------------------------------------------------------------}

procedure TEvents.ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
//OnConnect
writeln('connection established');

Client.Socket.SendText('NICK vogel');
Client.Socket.SendText('USER vogel localhost '+ Client.Host +' :masta vogel');

{
client.write('NICK vogel' +#13+#10);
client.write('USER vogel localhost '+ client.Host +' :masta vogel' +3+#10);
}
end;

procedure TEvents.ClientConnecting(Sender: TObject;
  Socket: TCustomWinSocket);
begin
//OnConnecting
writeln('connecting...');
end;

procedure TEvents.ClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
//OnDisconnect
writeln('disconnected');
end;

procedure TEvents.ClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
//OnError
writeln('Error Code: ' + inttostr(ErrorCode));
end;

procedure TEvents.ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  incoming: String;
begin
//OnRead
incoming := Socket.ReceiveText;

writeln(incoming);

{ping event reply}
if Copy(incoming,1,4) = 'PING' then
        begin
        sendout('PONG '+Copy(incoming,7,length(incoming)-6));
        writeln('ping replied');
        end;
end;


{------------------------------------------------------------------------------}

begin
  { TODO -oUser -cConsole Main : Insert code here }

{------------------------------------------------------------------------------}
{main code here}

writeln('Pepsi IRC Service v0.1*');



{------------------------------------------------------------------------------}

{catch commands}
while (lowercase(input) <> '/quit') do
        begin
        readln(input);

        if (lowercase(Copy(input,1,5)) = '/quit') then
                begin
                writeln('good bye');
                end
        else
        if (lowercase(Copy(input,1,8)) = '/connect') then
                begin
                connect;
                end

        else
        if (lowercase(copy(input,1,11)) = '/disconnect') then
                begin
                disconnect;
                end

        else
        if (Copy(input,1,1) = '.') then
                begin
                if Copy(input,2,length(input)-1) <> '' then
                        begin
                        sendout(Copy(input,2,length(input)-1));
                        end
                        else
                                begin
                                writeln('Syntax: .<text>');
                                end;
                end

        else
                begin
                writeln('not a command');
                end;

        end;

end.
  Mit Zitat antworten Zitat