Einzelnen Beitrag anzeigen

manuel

Registriert seit: 17. Sep 2003
Ort: MainBootRecord
72 Beiträge
 
Delphi 4 Client/Server
 
#1

TServerSocket Events werden nicht ausgeführt

  Alt 24. Nov 2004, 21:54
Hi Leute,

ich schreibe einen Windows-Service der nach einer eingehenden TCP-Verbindung Daten aus einer Datenbank holt und zurück zum Client sendet.

Und da liegt das Problem:
Der Client kann zwar verbinden, aber die TServerSocket Eventroutinen scheinen nicht aufgerufen zu werden

Noch ein paar Details:
Das Programm lief vorher als normales Anwender-Programm einwandfrei, dann habe ich versucht es in einen Service (threaded) zu konvertieren.

Der Serice erstellt einen Thread, welcher sich um die eigentliche Aufgabenbearbeitung kümmert.
Hier mal der Code für den Thread:


Delphi-Quellcode:
unit NTServiceThread;

interface

uses
  Windows, Messages, SysUtils, Classes,CheckSum, Help, AMC_Registry, Db, ZMySqlQuery,
  ZQuery, ZTransact, ZMySqlTr, ZConnect, ZMySqlCon, Log,ScktComp,Forms, Dialogs;


      type
  CustomRec = record
    StartTag: integer;
    MSG_1: String[255];
    MSG_2: String[255];
    CheckSum: Integer;
    EndTag: integer;
end;
   
type
  TNTServiceThread = Class(TThread)
  private
    { Private declarations } 
  Public
    { Public declarations } 
    Interval : Integer;
    SrvSock: TServerSocket; // der Server-Socket
    serverthread:TServerClientThread ;
    ReceiveRec: CustomRec ;
    SendRec : CustomRec ;
    Error_Counter:Integer;
    cstemp:String; //nimmt Einmal-Code auf
    help:THelp;
    logfile:TLog;
    registry: AMCRegistry;

    procedure Listening(Sender: TObject; Socket: TCustomWinSocket);
    procedure Connecting(Sender: TObject; Socket: TCustomWinSocket);
    procedure DisConnecting(Sender: TObject; Socket: TCustomWinSocket);
    procedure Reading(Sender: TObject; Socket: TCustomWinSocket);
    procedure IsError(Sender: TObject; Socket: TCustomWinSocket;
                     ErrorEvent: TErrorEvent; var ErrorCode: Integer);



    Procedure Execute; Override;
  Published
    { Published declarations } 
  End;

implementation

{ TNTServiceThread }
uses unit1;

procedure TNTServiceThread.Execute;
Var
   TimeOut : Integer;
begin
     { Do NOT free on termination - The Serivce frees the Thread } 
     FreeOnTerminate := False;

     { Set Interval } 
     TimeOut := Interval * 4;



help:=Thelp.Create;
registry:=AMCregistry.Create;
logfile:=TLog.create(true);
logfile.OpenLogFile;

 SrvSock:=TServerSocket.Create(unit1.DaVe_Licencing.Owner);
 logfile.WriteEntry('Create TServerSocket');
 SrvSock.Port:=registry.getPort;
 logfile.WriteEntry('Get Port');

 SrvSock.ServerType:=stNonBlocking;
 logfile.WriteEntry('Servertype stNonBlocking');
 SrvSock.OnListen:=Listening;
 SrvSock.OnClientConnect:=Connecting;
 SrvSock.OnClientDisConnect:=DisConnecting;
 SrvSock.OnClientRead:=Reading;
 SrvSock.Open;
 logfile.WriteEntry('ServSock.Open');
 registry.Free;
 beep;


     { Main Loop } 
     Try
        While Not Terminated do
        Begin
             { Decrement timeout } 
             Dec( TimeOut );

             If (TimeOut=0) then
             Begin
                  { Reset timer } 
                  TimeOut := Interval * 4;

                  { DoWork }








             End;
             { Wait 1/4th of a second } 
             Sleep(250);
        End;
     Except
        On E:Exception do ; // TODO: Exception logging...
     End;
     { Terminate the Thread - This signals Terminated=True }
     logfile.CloseLogFile;
     Terminate;
end;



procedure TNTServiceThread.IsError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
 Socket.Close;
 logfile.WriteEntry('Event: IsError - Socket.close');
 ErrorCode:=0;
end;

procedure TNTServiceThread.DisConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin

        logfile.WriteEntry('DisConnected: ' + Socket.RemoteHost+' '+Socket.RemoteAddress);
        beep;
end;


procedure TNTServiceThread.Connecting(Sender: TObject; Socket: TCustomWinSocket);


begin

 Socket.OnErrorEvent:=IsError;
 //Memo1.Lines.add('Connected to ' + Socket.RemoteHost+' '+Socket.RemoteAddress);
 logfile.WriteEntry('Connected to ' + Socket.RemoteHost+' '+Socket.RemoteAddress);
  beep;
end;

procedure TNTServiceThread.Listening(Sender: TObject; Socket: TCustomWinSocket);
begin

      // TODO
end;


procedure TNTServiceThread.Reading(Sender: TObject; Socket: TCustomWinSocket);
var Licence:String;
begin
showMessage('#Incoming');
Socket.ReceiveBuf (ReceiveRec,sizeof(ReceiveRec));
logfile.WriteEntry(ReceiveRec.MSG_1);
end;

end.

Gibt es eigentlich eine Sinnvolle Möglichkeit (ausser loggen in txt-files) Services zu debuggen?


Gruß

Manuel
sometimes I feel diagonally parked in a parallel universe
  Mit Zitat antworten Zitat