[ROOT] Spyserver tutorial

From: cstrato@EUnet.at
Date: Tue Mar 26 2002 - 22:26:01 MET


Dear Rene, dear Fons

For testing purposes I have changed spyserver.C into a standalone
application (see code fragments below).
Now I have the following questions:

1, TSocket::Recv(..):
If (s->Recv(request) <= 0) then the socket is removed from TMonitor:
However, only (message->What() == 0) means that the connection is
closed.
How should a real application handle the cases:
(what == -1) and (what == -4) ?

2, SpyServer::Run():
Can an infinite loop be used in TApplication::Run() as shown below,
or would it be better to implement a TFileHandler?
However, TMonitor already uses TSocketHandler. Is this sufficient?

3, TFileHandler:
In general, when should a class inherited from TFileHandler be used?

Thank you in advance

Best regards
Christian
----------------------------------
C.h.r.i.s.t.i.a.n  S.t.r.a.t.o.w.a
V.i.e.n.n.a,  A.u.s.t.r.i.a


//----spyserv.h-----
class SpyServer: public TApplication {
   private:
      TServerSocket *fServ;
      TMonitor      *fMon;
      TList         *fSockets;

   public:
      SpyServer(int *argc, char **argv);
      ~SpyServer();

      void HandleSocket(TSocket *s);
      void Run(Bool_t retrn = kFALSE);

      ClassDef(SpyServer,1) //
};


//----spyserv.cxx-----
void SpyServer::HandleSocket(TSocket *s)
{
   if (s->IsA() == TServerSocket::Class()) {
      TSocket *sock = ((TServerSocket*)s)->Accept();
      fMon->Add(sock);
      fSockets->Add(sock);
   } else {
      char request[64];
      if (s->Recv(request, sizeof(request)) <= 0) {
         fMon->Remove(s);
         fSockets->Remove(s);
         delete s;
         return;
      }
  }
}

void SpyServer::Run(Bool_t retrn)
{
   for (Int_t i = 0; ; i++) {
      TSocket *s;
      if ((s = fMon->Select(20)) != (TSocket*)-1)
         HandleSocket(s);
      if (gROOT->IsInterrupted())
         break;
   }
}


//----spymain.cxx-----
int main(int argc, char **argv)
{
  gROOT->SetBatch();
   SpyServer *theApp = new SpyServer(&argc, argv);

   theApp->Run();

   delete theApp;

   return 0;
}



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:47 MET