Thema: Delphi Zuweisen eines Events?!

Einzelnen Beitrag anzeigen

Astat

Registriert seit: 2. Dez 2009
Ort: München
320 Beiträge
 
Lazarus
 
#5

Re: Zuweisen eines Events?!

  Alt 3. Mär 2010, 00:01
Hallo Berlinermauer, in deinem geposteten Code, ist einiges nicht ganz optimal implementiert.

Für eine Asynchrone Übertragung, kann folgendes Template verwendet werden.

Protokoll Stack, Errorbehandlung müssen noch implementiert werden.

Als konzeptioneller Ansatz, sollte das Teil aber brauchbar sein.


Delphi-Quellcode:

program ConsoleClient;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages,
  SysUtils,
  Winsock,
  ScktComp;

type
  TClientSocketSample = class
  private
    FClientSocket: TClientSocket;
    procedure ClientSocketConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketConnecting(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocketLookup(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocketWrite(Sender: TObject;
      Socket: TCustomWinSocket);
  public
    constructor create;
    destructor destroy; override;
  end;


var
  ClientSocketSample: TClientSocketSample;
  MsgRec : TMsg;

{ TClientSocketSample }

procedure TClientSocketSample.ClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  writeln(
    'Ein Client hat sich Server verbunden. Socket: ' +
      inttostr(Socket.SocketHandle)
      );
end;

procedure TClientSocketSample.ClientSocketConnecting(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  writeln(
    'Ein Client is connecting to Server Socket: ' +
      inttostr(Socket.SocketHandle)
      );
end;

procedure TClientSocketSample.ClientSocketDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  writeln(
    'Ein Client disconnect from Server. Socket: ' +
      inttostr(Socket.SocketHandle)
      );
end;

procedure TClientSocketSample.ClientSocketError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  writeln(
    'Ein Clientverbindung mit folgendem eindeutigen Socket: ' +
      inttostr(Socket.SocketHandle) +
        ' hat einen Fehler verursacht, der socket Fehlercode:' +
          inttostr(ErrorCode)
      );
end;

procedure TClientSocketSample.ClientSocketLookup(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  writeln(
    'Ein Client Lookup for Server. Socket: ' +
      inttostr(Socket.SocketHandle)
      );
end;

procedure TClientSocketSample.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  cbRcv: integer;
  RcvBuf: array [0..32767] of Byte;
  s: string;
begin
  cbRcv := Socket.ReceiveBuf(RcvBuf[0], Length(RcvBuf));
  if (cbRcv > 0) and (Socket.SocketHandle > 0) then begin
    SetLength(s, cbRcv);
    move(RcvBuf[0], s[1], cbRcv);
    writeln('Vom Server mit folgendem eindeutigen Socket: ' +
      inttostr(Socket.SocketHandle) + ' wurden ' + IntToStr(cbRcv) + ' Bytes ' +
        'mit folgenden Daten [ ' + s + ' ] empfangen'
        );
  end;
end;

procedure TClientSocketSample.ClientSocketWrite(Sender: TObject;
  Socket: TCustomWinSocket);
var
  s: string;
begin
  s := 'Hello World, this is from Client';
  Socket.SendBuf(s[1], Length(s));
end;

constructor TClientSocketSample.create;
begin
  Self.FClientSocket := TClientSocket.create(nil);
  with Self.FClientSocket, Self do begin
    Port := 8088;
    Address := '127.0.0.1';
    OnConnect := ClientSocketConnect;
    OnDisconnect := ClientSocketDisconnect;
    OnError := ClientSocketError;
    OnRead := ClientSocketRead;
    OnConnecting := ClientSocketConnecting;
    OnLookUp := ClientSocketLookUp;
    OnWrite := ClientSocketWrite;
    Active := true;
  end;
end;

destructor TClientSocketSample.destroy;
begin
  shutdown(Self.FClientSocket.Socket.SocketHandle, SD_BOTH);
  CloseSocket(Self.FClientSocket.Socket.SocketHandle);
  inherited;
end;


begin
  ClientSocketSample := TClientSocketSample.Create;
  try
    repeat
      if PeekMessage(MsgRec, 0, 0, 0, PM_REMOVE) then
        DispatchMessage(MsgRec) else WaitMessage;
    until (MsgRec.Message = WM_QUIT) or (MsgRec.Message = WM_CLOSE);
  finally
    ClientSocketSample.Free;
    ClientSocketSample := nil;
  end;
end.
lg. Astat
Lanthan Astat
06810110811210410503210511511603209711003210010110 9032084097103
03211611111604403209711003210010110903210010510103 2108101116122
11610103209010110510810103206711110010103210511003 2068101108112
10410503210310111509910411410510109810111003211910 5114100046
  Mit Zitat antworten Zitat