ROOT logo

From $ROOTSYS/tutorials/net/hserv2.C

{
   // This script shows how to make a simple iterative server that
   // can accept connections while handling currently open connections.
   // Compare this script to hserv.C that blocks on accept.
   // In this script a server socket is created and added to a monitor.
   // A monitor object is used to monitor connection requests on
   // the server socket. After accepting the connection
   // the new socket is added to the monitor and immediately ready
   // for use. Once two connections are accepted the server socket
   // is removed from the monitor and closed. The monitor continues
   // monitoring the sockets.
   //
   // To run this demo do the following:
   //   - Open three windows
   //   - Start ROOT in all three windows
   //   - Execute in the first window: .x hserv2.C
   //   - Execute in the second and third windows: .x hclient.C
   //Author: Fons Rademakers
   
   // Create canvas and pads to display the histograms
   TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
   TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
   TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
   pad1->Draw();
   pad2->Draw();

   // Open a server socket looking for connections on a named service or
   // on a specified port.
   //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
   TServerSocket *ss = new TServerSocket(9090, kTRUE);

   TMonitor *mon = new TMonitor;

   mon->Add(ss);

   TSocket *s0 = 0, *s1 = 0;

   while (1) {
      TMessage *mess;
      TSocket  *s;

      s = mon->Select();

      if (s->IsA() == TServerSocket::Class()) {
         if (!s0) {
            s0 = ((TServerSocket *)s)->Accept();
            s0->Send("go 0");
            mon->Add(s0);
         } else if (!s1) {
            s1 = ((TServerSocket *)s)->Accept();
            s1->Send("go 1");
            mon->Add(s1);
         } else
            printf("only accept two client connections\n");

         if (s0 && s1) {
            mon->Remove(ss);
            ss->Close();
         }
         continue;
      }

      s->Recv(mess);

      if (mess->What() == kMESS_STRING) {
         char str[64];
         mess->ReadString(str, 64);
         printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
         mon->Remove(s);
         if (mon->GetActive() == 0) {
            printf("No more active clients... stopping\n");
            break;
         }
      } else if (mess->What() == kMESS_OBJECT) {
         //printf("got object of class: %s\n", mess->GetClass()->GetName());
         TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
         if (h) {
            if (s == s0)
               pad1->cd();
            else
               pad2->cd();
            h->Print();
            h->DrawCopy();  //draw a copy of the histogram, not the histo itself
            c1->Modified();
            c1->Update();
            delete h;       // delete histogram
         }
      } else {
         printf("*** Unexpected message ***\n");
      }

      delete mess;
   }

   printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
          s0->GetBytesSent());
   printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
          s1->GetBytesSent());

   // Close the socket.
   s0->Close();
   s1->Close();
}
 hserv2.C:1
 hserv2.C:2
 hserv2.C:3
 hserv2.C:4
 hserv2.C:5
 hserv2.C:6
 hserv2.C:7
 hserv2.C:8
 hserv2.C:9
 hserv2.C:10
 hserv2.C:11
 hserv2.C:12
 hserv2.C:13
 hserv2.C:14
 hserv2.C:15
 hserv2.C:16
 hserv2.C:17
 hserv2.C:18
 hserv2.C:19
 hserv2.C:20
 hserv2.C:21
 hserv2.C:22
 hserv2.C:23
 hserv2.C:24
 hserv2.C:25
 hserv2.C:26
 hserv2.C:27
 hserv2.C:28
 hserv2.C:29
 hserv2.C:30
 hserv2.C:31
 hserv2.C:32
 hserv2.C:33
 hserv2.C:34
 hserv2.C:35
 hserv2.C:36
 hserv2.C:37
 hserv2.C:38
 hserv2.C:39
 hserv2.C:40
 hserv2.C:41
 hserv2.C:42
 hserv2.C:43
 hserv2.C:44
 hserv2.C:45
 hserv2.C:46
 hserv2.C:47
 hserv2.C:48
 hserv2.C:49
 hserv2.C:50
 hserv2.C:51
 hserv2.C:52
 hserv2.C:53
 hserv2.C:54
 hserv2.C:55
 hserv2.C:56
 hserv2.C:57
 hserv2.C:58
 hserv2.C:59
 hserv2.C:60
 hserv2.C:61
 hserv2.C:62
 hserv2.C:63
 hserv2.C:64
 hserv2.C:65
 hserv2.C:66
 hserv2.C:67
 hserv2.C:68
 hserv2.C:69
 hserv2.C:70
 hserv2.C:71
 hserv2.C:72
 hserv2.C:73
 hserv2.C:74
 hserv2.C:75
 hserv2.C:76
 hserv2.C:77
 hserv2.C:78
 hserv2.C:79
 hserv2.C:80
 hserv2.C:81
 hserv2.C:82
 hserv2.C:83
 hserv2.C:84
 hserv2.C:85
 hserv2.C:86
 hserv2.C:87
 hserv2.C:88
 hserv2.C:89
 hserv2.C:90
 hserv2.C:91
 hserv2.C:92
 hserv2.C:93
 hserv2.C:94
 hserv2.C:95
 hserv2.C:96
 hserv2.C:97
 hserv2.C:98
 hserv2.C:99
 hserv2.C:100
 hserv2.C:101
 hserv2.C:102
 hserv2.C:103
 hserv2.C:104