Logo ROOT   6.07/09
Reference Guide
hserv.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_net
3 /// Server program which waits for two clients to connect. It then monitors
4 /// the sockets and displays the objects it receives. To see how to
5 /// make a non-blocking server see the script hserv2.C.
6 ///
7 /// To run this demo do the following:
8 /// - Open three windows
9 /// - Start ROOT in all three windows
10 /// - Execute in the first window: .x hserv.C
11 /// - Execute in the second and third windows: .x hclient.C
12 ///
13 /// \macro_code
14 ///
15 /// \author Fons Rademakers
16 
17 void hserv() {
18  // Open a server socket looking for connections on a named service or
19  // on a specified port.
20  //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
21  TServerSocket *ss = new TServerSocket(9090, kTRUE);
22 
23  // Accept a connection and return a full-duplex communication socket.
24  TSocket *s0 = ss->Accept();
25  TSocket *s1 = ss->Accept();
26 
27  // tell the clients to start
28  s0->Send("go 0");
29  s1->Send("go 1");
30 
31  // Close the server socket (unless we will use it later to wait for
32  // another connection).
33  ss->Close();
34 
35  // Check some options of socket 0.
36  int val;
37  s0->GetOption(kSendBuffer, val);
38  printf("sendbuffer size: %d\n", val);
39  s0->GetOption(kRecvBuffer, val);
40  printf("recvbuffer size: %d\n", val);
41 
42  // Get the remote addresses (informational only).
43  TInetAddress adr = s0->GetInetAddress();
44  adr.Print();
45  adr = s1->GetInetAddress();
46  adr.Print();
47 
48  // Create canvas and pads to display the histograms
49  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
50  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
51  TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
52  pad1->Draw();
53  pad2->Draw();
54 
55  TMonitor *mon = new TMonitor;
56 
57  mon->Add(s0);
58  mon->Add(s1);
59 
60  while (1) {
61  TMessage *mess;
62  TSocket *s;
63 
64  s = mon->Select();
65 
66  s->Recv(mess);
67 
68  if (mess->What() == kMESS_STRING) {
69  char str[64];
70  mess->ReadString(str, 64);
71  printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
72  mon->Remove(s);
73  if (mon->GetActive() == 0) {
74  printf("No more active clients... stopping\n");
75  break;
76  }
77  } else if (mess->What() == kMESS_OBJECT) {
78  //printf("got object of class: %s\n", mess->GetClass()->GetName());
79  TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
80  if (h) {
81  if (s == s0)
82  pad1->cd();
83  else
84  pad2->cd();
85  h->Print();
86  h->DrawCopy(); //draw a copy of the histogram, not the histo itself
87  c1->Modified();
88  c1->Update();
89  delete h; // delete histogram
90  }
91  } else {
92  printf("*** Unexpected message ***\n");
93  }
94 
95  delete mess;
96  }
97 
98  printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
99  s0->GetBytesSent());
100  printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
101  s1->GetBytesSent());
102 
103  // Close the socket.
104  s0->Close();
105  s1->Close();
106 }
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
Definition: TMonitor.cxx:214
return c1
Definition: legend1.C:41
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
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
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:526
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:2899
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1208
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:6281
The most important graphics class in the ROOT system.
Definition: TPad.h:37
The Canvas class.
Definition: TCanvas.h:41
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
Definition: TMonitor.cxx:438
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
TClass * GetClass() const
Definition: TMessage.h:76
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
TInetAddress GetInetAddress() const
Definition: TSocket.h:143
const Bool_t kTRUE
Definition: Rtypes.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:399