Logo ROOT   6.16/01
Reference Guide
hserv2bonj.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 // Advertise our service using Bonjour
34 reg->RegisterService(TBonjourRecord(Form("hserv2 on %s", gSystem->HostName()),
35 "_hserv2._tcp", ""), 9090);
36
37 // Open a server socket looking for connections on a named service or
38 // on a specified port.
39 //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
40 TServerSocket *ss = new TServerSocket(9090, kTRUE);
41
42 TMonitor *mon = new TMonitor;
43
44 mon->Add(ss);
45
46 TSocket *s0 = 0, *s1 = 0;
47
48 while (1) {
49 TMessage *mess;
50 TSocket *s;
51
52 s = mon->Select();
53
54 if (s->IsA() == TServerSocket::Class()) {
55 if (!s0) {
56 s0 = ((TServerSocket *)s)->Accept();
57 s0->Send("go 0");
58 mon->Add(s0);
59 } else if (!s1) {
60 s1 = ((TServerSocket *)s)->Accept();
61 s1->Send("go 1");
62 mon->Add(s1);
63 } else
64 printf("only accept two client connections\n");
65
66 if (s0 && s1) {
67 mon->Remove(ss);
68 ss->Close();
69 }
70 continue;
71 }
72
73 s->Recv(mess);
74
75 if (mess->What() == kMESS_STRING) {
76 char str[64];
77 mess->ReadString(str, 64);
78 printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
79 mon->Remove(s);
80 if (mon->GetActive() == 0) {
81 printf("No more active clients... stopping\n");
82 break;
83 }
84 } else if (mess->What() == kMESS_OBJECT) {
85 //printf("got object of class: %s\n", mess->GetClass()->GetName());
86 TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
87 if (h) {
88 if (s == s0)
89 pad1->cd();
90 else
91 pad2->cd();
92 h->Print();
93 h->DrawCopy(); //draw a copy of the histogram, not the histo itself
94 c1->Modified();
95 c1->Update();
96 delete h; // delete histogram
97 }
98 } else {
99 printf("*** Unexpected message ***\n");
100 }
101
102 delete mess;
103 }
104
105 printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
106 s0->GetBytesSent());
107 printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
108 s1->GetBytesSent());
109
110 // Close the socket.
111 s0->Close();
112 s1->Close();
113}
void Class()
Definition: Class.C:29
@ kMESS_STRING
Definition: MessageTypes.h:34
@ kMESS_OBJECT
Definition: MessageTypes.h:35
#define s0(x)
Definition: RSha256.hxx:90
#define s1(x)
Definition: RSha256.hxx:91
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kTRUE
Definition: RtypesCore.h:87
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Int_t RegisterService(const TBonjourRecord &record, UShort_t servicePort)
Register Bonjour service.
virtual TObject * ReadObject(const TClass *cl)
Read object from I/O buffer.
virtual char * ReadString(char *s, Int_t max)
Read string from I/O buffer.
The Canvas class.
Definition: TCanvas.h:31
The TH1 histogram class.
Definition: TH1.h:56
UInt_t What() const
Definition: TMessage.h:75
TClass * GetClass() const
Definition: TMessage.h:71
TSocket * Select()
Return pointer to socket for which an event is waiting.
Definition: TMonitor.cxx:322
virtual void Add(TSocket *sock, Int_t interest=kRead)
Add socket to the monitor's active list.
Definition: TMonitor.cxx:168
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
Definition: TMonitor.cxx:438
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
Definition: TMonitor.cxx:214
The most important graphics class in the ROOT system.
Definition: TPad.h:29
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1281
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:389
virtual const char * HostName()
Return the system's host name.
Definition: TSystem.cxx:312
return c1
Definition: legend1.C:41
static constexpr double s