ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hserv.C
Go to the documentation of this file.
1 void hserv() {
2  // Server program which waits for two clients to connect. It then monitors
3  // the sockets and displays the objects it receives. To see how to
4  // make a non-blocking server see the script hserv2.C.
5  //
6  // To run this demo do the following:
7  // - Open three windows
8  // - Start ROOT in all three windows
9  // - Execute in the first window: .x hserv.C
10  // - Execute in the second and third windows: .x hclient.C
11  //Author: Fons Rademakers
12 
13  // Open a server socket looking for connections on a named service or
14  // on a specified port.
15  //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
16  TServerSocket *ss = new TServerSocket(9090, kTRUE);
17 
18  // Accept a connection and return a full-duplex communication socket.
19  TSocket *s0 = ss->Accept();
20  TSocket *s1 = ss->Accept();
21 
22  // tell the clients to start
23  s0->Send("go 0");
24  s1->Send("go 1");
25 
26  // Close the server socket (unless we will use it later to wait for
27  // another connection).
28  ss->Close();
29 
30  // Check some options of socket 0.
31  int val;
32  s0->GetOption(kSendBuffer, val);
33  printf("sendbuffer size: %d\n", val);
34  s0->GetOption(kRecvBuffer, val);
35  printf("recvbuffer size: %d\n", val);
36 
37  // Get the remote addresses (informational only).
38  TInetAddress adr = s0->GetInetAddress();
39  adr.Print();
40  adr = s1->GetInetAddress();
41  adr.Print();
42 
43  // Create canvas and pads to display the histograms
44  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
45  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
46  TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
47  pad1->Draw();
48  pad2->Draw();
49 
50  TMonitor *mon = new TMonitor;
51 
52  mon->Add(s0);
53  mon->Add(s1);
54 
55  while (1) {
56  TMessage *mess;
57  TSocket *s;
58 
59  s = mon->Select();
60 
61  s->Recv(mess);
62 
63  if (mess->What() == kMESS_STRING) {
64  char str[64];
65  mess->ReadString(str, 64);
66  printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
67  mon->Remove(s);
68  if (mon->GetActive() == 0) {
69  printf("No more active clients... stopping\n");
70  break;
71  }
72  } else if (mess->What() == kMESS_OBJECT) {
73  //printf("got object of class: %s\n", mess->GetClass()->GetName());
74  TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
75  if (h) {
76  if (s == s0)
77  pad1->cd();
78  else
79  pad2->cd();
80  h->Print();
81  h->DrawCopy(); //draw a copy of the histogram, not the histo itself
82  c1->Modified();
83  c1->Update();
84  delete h; // delete histogram
85  }
86  } else {
87  printf("*** Unexpected message ***\n");
88  }
89 
90  delete mess;
91  }
92 
93  printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
94  s0->GetBytesSent());
95  printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
96  s1->GetBytesSent());
97 
98  // Close the socket.
99  s0->Close();
100  s1->Close();
101 }
TServerSocket * ss
Definition: hserv2.C:30
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
Definition: TMonitor.cxx:214
TMonitor * mon
Definition: hserv2.C:32
TCanvas * c1
Definition: legend1.C:2
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:520
Option_t * GetOption() const
Definition: TSocket.h:128
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
TH1 * h
Definition: legend2.C:5
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:40
TPad * pad1
Definition: hcons.C:13
virtual void Add(TSocket *sock, Int_t interest=kRead)
Add socket to the monitor's active list.
Definition: TMonitor.cxx:168
UInt_t GetBytesSent() const
Definition: TSocket.h:149
TSocket * s1
Definition: hserv2.C:36
UInt_t GetBytesRecv() const
Definition: TSocket.h:150
virtual TObject * ReadObject(const TClass *cl)
Read object from I/O buffer.
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
virtual char * ReadString(char *s, Int_t max)
Read string from I/O buffer.
virtual TSocket * Accept(UChar_t Opt=0)
Accept a connection on a server socket.
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:2925
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
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:388
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6573
The most important graphics class in the ROOT system.
Definition: TPad.h:46
The Canvas class.
Definition: TCanvas.h:48
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
Definition: TMonitor.cxx:438
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void Print(Option_t *option="") const
Print internet address as string.
The TH1 histogram class.
Definition: TH1.h:80
UInt_t What() const
Definition: TMessage.h:80
TPad * pad2
Definition: hcons.C:13
TSocket * s0
Definition: hserv2.C:36
TClass * GetClass() const
Definition: TMessage.h:76
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
TInetAddress GetInetAddress() const
Definition: TSocket.h:143
const Bool_t kTRUE
Definition: Rtypes.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:407
void hserv()
Definition: hserv.C:1