Einzelnen Beitrag anzeigen

Beckman

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

Re: console application --> events --> clientsocket pr

  Alt 16. Aug 2004, 14:33
so, jetzt v3: cs03

es bleibt immer stecken beim "procedure TMyThread.Execute;"
und zwar genau beim procedure "Synchronize(Main);"

kann mir jemand damit helfen?

Code:
program cs;

{$APPTYPE CONSOLE}

uses
  SysUtils, Forms, Classes,
  ScktComp;

type
  TEvent = class
  private
    FClient: TClientSocket;
  public
    constructor Create;
    destructor Destroy; override;
    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);
    procedure Connect;
    procedure Disconnect;
    procedure Send(Msg: string; const Prefix: string = #13#10);
  end;

  TMyThread = class(TThread)
  private
    procedure Main;
  public
    procedure Execute; override;
  end;

var
  gEvent: TEvent;
  gInput: string;

// errorcodetostring
//
function ErrorCodeToString(ErrorCode: integer): string;
begin
  case ErrorCode of
    10004: Result := 'interrupted function call';
    10013: Result := 'permission denied';
    10014: Result := 'bad address';
    10022: Result := 'invalid argument';
    10024: Result := 'too many open files';
    10035: Result := 'resource temporarily unavailable';
    10036: Result := 'operation now in progress';
    10037: Result := 'operation already in progress';
    10038: Result := 'socket operation on non-socket';
    10039: Result := 'destination address required';
    10040: Result := 'message too long';
    10041: Result := 'protocol wrong type for socket';
    10042: Result := 'bad protocol option';
    10043: Result := 'protocol not supported';
    10044: Result := 'socket type not supported';
    10045: Result := 'operation not supported';
    10046: Result := 'protocol family not supported';
    10047: Result := 'address family not supported by protocol family';
    10048: Result := 'address already in use';
    10049: Result := 'cannot assign requested address';
    10050: Result := 'network is down';
    10051: Result := 'network is unreachable';
    10052: Result := 'network dropped connection on reset';
    10053: Result := 'software caused connection abort';
    10054: Result := 'connection reset by peer';
    10055: Result := 'no buffer space available';
    10056: Result := 'socket is already connected';
    10057: Result := 'socket is not connected';
    10058: Result := 'cannot send after socket shutdown';
    10060: Result := 'connection timed out';
    10061: Result := 'connection refused';
    10064: Result := 'host is down';
    10065: Result := 'no route to host';
    10067: Result := 'too many processes';
    10091: Result := 'network subsystem is unavailable';
    10092: Result := 'winsock.dll version out of range';
    10093: Result := 'successful wsastartup not yet performed';
    10094: Result := 'graceful shutdown in progress';
    11001: Result := 'host not found';
    11002: Result := 'non-authoritative host not found';
    11003: Result := 'this is a non-recoverable error';
    11004: Result := 'valid name, no data record of requested type';
  end;
end;

{ TEvent }

// create
//
constructor TEvent.Create;
begin
  inherited Create;
  FClient := TClientSocket.Create(nil);
  WriteLn('client created');
  FClient.OnConnect := ClientConnect;
  if Assigned(FClient.OnConnect) then
    WriteLn(':: assigend event - clientconnect');
  FClient.OnConnecting := ClientConnecting;
  if Assigned(FClient.OnConnecting) then
    WriteLn(':: assigned event - clientconnecting');
  FClient.OnDisconnect := ClientDisconnect;
  if Assigned(FClient.OnDisconnect) then
    WriteLn(':: assigned event - clientdisconnect');
  FClient.OnError := ClientError;
  if Assigned(FClient.OnError) then
    WriteLn(':: assigned event - clienterror');
  FClient.OnRead := ClientRead;
  if Assigned(FClient.OnRead) then
    WriteLn(':: assigned event - clientread');
end;

// destroy
//
destructor TEvent.Destroy;
begin
  WriteLn('disposing client');
  if FClient.Active then
    FClient.Active := False;
  FClient.OnConnect := nil;
  FClient.OnConnecting := nil;
  FClient.OnDisconnect := nil;
  FClient.OnError := nil;
  FClient.OnRead := nil;
  FreeAndNil(FClient);
  WriteLn('client disposed');
end;

// clientconnect
//
procedure TEvent.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  WriteLn('connection established');
end;

// clientconnecting
//
procedure TEvent.ClientConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin
  WriteLn('connecting...');
end;

// clientdisconnect
//
procedure TEvent.ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  WriteLn('disconnected');
end;

// clienterror
//
procedure TEvent.ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  WriteLn('error: ' + ErrorCodeToString(ErrorCode));
end;

// clientread
//
procedure TEvent.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  lReceiveText: string;

begin
  SetLength(lReceiveText, Socket.ReceiveLength);
  lReceiveText := Socket.ReceiveText;
  WriteLn('read event!');
  WriteLn(lReceiveText);
end;

// connect
//
procedure TEvent.Connect;
begin
  WriteLn('connect() executed!');
  FClient.Host := '127.0.0.1';
  FClient.Port := 6668;
//  try
//    try
//      FClient.Active := True;
//      WriteLn('trying to set client active');
//    finally
//      WriteLn('client is active');
//    end;
//  except
//    WriteLn('could not set client active, trying other method');
//  end;
//  try
//    try
//      FClient.Open;
//      WriteLn('trying to open client');;
//   finally
//      WriteLn('client is open');
//    end;
//  except
//    WriteLn('could not open client');
//  end;
  FClient.Active := True;
end;

// disconnect
//
procedure TEvent.Disconnect;
begin
  if FClient.Active then
    FClient.Active := False;
  WriteLn('[disconnect] client is inactive');
end;

// send
//
procedure TEvent.Send(Msg: string; const Prefix: string = #13#10);
begin
  if FClient.Active then begin
    FClient.Socket.SendText(Msg + Prefix);
  end
  else
    WriteLn('[send] client is inactive');
end;

{ TMyThread }

// execute
//
procedure TMyThread.Execute;
begin
  writeln('doing sync');
  Synchronize(Main);
  writeln('done sync');

end;

// main
//
procedure TMyThread.Main;
begin
  while not Terminated do begin
    ReadLn(gInput);
    if gInput = 'connect' then
      gEvent.Connect
    else if gInput = 'disconnect' then
      gEvent.Disconnect
    else if gInput = 'shutdown' then
      Exit
    else
      gEvent.Send(gInput);
    Application.ProcessMessages;
  end;
end;

var
  gMyThread: TMyThread;

begin
  gEvent := TEvent.Create;
{
  repeat
    ReadLn(gInput);
    if gInput = 'connect' then
      gEvent.Connect
    else if gInput = 'disconnect' then
      gEvent.Disconnect
    else
      gEvent.Send(gInput);
  until gInput = 'shutdown';
}
{
  while True do begin
    ReadLn(gInput);
    if gInput = 'connect' then
      gEvent.Connect
    else if gInput = 'disconnect' then
      gEvent.Disconnect
    else if gInput = 'shutdown' then
      Exit
    else
      gEvent.Send(gInput);
    Application.ProcessMessages;
  end;
}
  gMyThread := TMyThread.Create(False);
  gMyThread.Execute;
  FreeAndNil(gEvent);
  ReadLn;
end.
  Mit Zitat antworten Zitat