Logo ROOT   6.14/05
Reference Guide
hserv2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_net
3 /// This script shows how to make a simple iterative server that
4 /// can accept connections while handling currently open connections.
5 /// Compare this script to hserv.C that blocks on accept.
6 /// In this script a server socket is created and added to a monitor.
7 /// A monitor object is used to monitor connection requests on
8 /// the server socket. After accepting the connection
9 /// the new socket is added to the monitor and immediately ready
10 /// for use. Once two connections are accepted the server socket
11 /// is removed from the monitor and closed. The monitor continues
12 /// monitoring the sockets.
13 ///
14 /// To run this demo do the following:
15 /// - Open three windows
16 /// - Start ROOT in all three windows
17 /// - Execute in the first window: .x hserv2.C
18 /// - Execute in the second and third windows: .x hclient.C
19 ///
20 /// \macro_code
21 ///
22 /// \author Fons Rademakers
23 
24 {
25  // Create canvas and pads to display the histograms
26  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
27  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
28  TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
29  pad1->Draw();
30  pad2->Draw();
31 
32  // Open a server socket looking for connections on a named service or
33  // on a specified port.
34  //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
35  TServerSocket *ss = new TServerSocket(9090, kTRUE);
36 
37  TMonitor *mon = new TMonitor;
38 
39  mon->Add(ss);
40 
41  TSocket *s0 = 0, *s1 = 0;
42 
43  while (1) {
44  TMessage *mess;
45  TSocket *s;
46 
47  s = mon->Select();
48 
49  if (s->IsA() == TServerSocket::Class()) {
50  if (!s0) {
51  s0 = ((TServerSocket *)s)->Accept();
52  s0->Send("go 0");
53  mon->Add(s0);
54  } else if (!s1) {
55  s1 = ((TServerSocket *)s)->Accept();
56  s1->Send("go 1");
57  mon->Add(s1);
58  } else
59  printf("only accept two client connections\n");
60 
61  if (s0 && s1) {
62  mon->Remove(ss);
63  ss->Close();
64  }
65  continue;
66  }
67 
68  s->Recv(mess);
69 
70  if (mess->What() == kMESS_STRING) {
71  char str[64];
72  mess->ReadString(str, 64);
73  printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
74  mon->Remove(s);
75  if (mon->GetActive() == 0) {
76  printf("No more active clients... stopping\n");
77  break;
78  }
79  } else if (mess->What() == kMESS_OBJECT) {
80  //printf("got object of class: %s\n", mess->GetClass()->GetName());
81  TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
82  if (h) {
83  if (s == s0)
84  pad1->cd();
85  else
86  pad2->cd();
87  h->Print();
88  h->DrawCopy(); //draw a copy of the histogram, not the histo itself
89  c1->Modified();
90  c1->Update();
91  delete h; // delete histogram
92  }
93  } else {
94  printf("*** Unexpected message ***\n");
95  }
96 
97  delete mess;
98  }
99 
100  printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
101  s0->GetBytesSent());
102  printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
103  s1->GetBytesSent());
104 
105  // Close the socket.
106  s0->Close();
107  s1->Close();
108 }
TClass * GetClass() const
Definition: TMessage.h:70
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6489
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
Definition: TMonitor.cxx:214
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
Definition: TMonitor.cxx:438
UInt_t GetBytesRecv() const
Definition: TSocket.h:139
#define s0(x)
Definition: RSha256.hxx:90
return c1
Definition: legend1.C:41
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:527
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:822
virtual void Add(TSocket *sock, Int_t interest=kRead)
Add socket to the monitor's active list.
Definition: TMonitor.cxx:168
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:3021
virtual TObject * ReadObject(const TClass *cl)
Read object from I/O buffer.
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
void Class()
Definition: Class.C:29
virtual char * ReadString(char *s, Int_t max)
Read string from I/O buffer.
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1281
TSocket * Select()
Return pointer to socket for which an event is waiting.
Definition: TMonitor.cxx:322
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:394
UInt_t GetBytesSent() const
Definition: TSocket.h:138
The most important graphics class in the ROOT system.
Definition: TPad.h:29
#define s1(x)
Definition: RSha256.hxx:91
#define h(i)
Definition: RSha256.hxx:106
UInt_t What() const
Definition: TMessage.h:74
The Canvas class.
Definition: TCanvas.h:31
The TH1 histogram class.
Definition: TH1.h:56
static constexpr double s
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
const Bool_t kTRUE
Definition: RtypesCore.h:87
void Modified(Bool_t flag=1)
Definition: TPad.h:414