Logo ROOT  
Reference Guide
No Matches
hserv.C File Reference

Detailed Description

Server program which waits for two clients to connect.

It then monitors the sockets and displays the objects it receives. To see how to make a non-blocking server see the script hserv2.C.

To run this demo do the following:

  • Open three windows
  • Start ROOT in all three windows
  • Execute in the first window: .x hserv.C
  • Execute in the second and third windows: .x hclient.C
void hserv() {
// 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);
// Accept a connection and return a full-duplex communication socket.
TSocket *s0 = ss->Accept();
TSocket *s1 = ss->Accept();
// tell the clients to start
s0->Send("go 0");
s1->Send("go 1");
// Close the server socket (unless we will use it later to wait for
// another connection).
// Check some options of socket 0.
int val;
s0->GetOption(kSendBuffer, val);
printf("sendbuffer size: %d\n", val);
s0->GetOption(kRecvBuffer, val);
printf("recvbuffer size: %d\n", val);
// Get the remote addresses (informational only).
TInetAddress adr = s0->GetInetAddress();
adr = s1->GetInetAddress();
// 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);
TMonitor *mon = new TMonitor;
while (1) {
TMessage *mess;
TSocket *s;
s = mon->Select();
if (mess->What() == kMESS_STRING) {
char str[64];
mess->ReadString(str, 64);
printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
if (mon->GetActive() == 0) {
printf("No more active clients... stopping\n");
} 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)
h->DrawCopy(); //draw a copy of the histogram, not the histo itself
delete h; // delete histogram
} else {
printf("*** Unexpected message ***\n");
delete mess;
printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
// Close the socket.
#define s0(x)
Definition RSha256.hxx:90
#define s1(x)
Definition RSha256.hxx:91
#define h(i)
Definition RSha256.hxx:106
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
@ kSendBuffer
Definition TSystem.h:216
@ kRecvBuffer
Definition TSystem.h:217
TObject * ReadObject(const TClass *cl) override
Read object from I/O buffer.
char * ReadString(char *s, Int_t max) override
Read string from I/O buffer.
The Canvas class.
Definition TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
This class represents an Internet Protocol (IP) address.
void Print(Option_t *option="") const override
Print internet address as string.
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:28
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:597
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1268
virtual TSocket * Accept(UChar_t Opt=0)
Accept a connection on a server socket.
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition TSocket.cxx:818
virtual void Close(Option_t *opt="")
Close the socket.
Definition TSocket.cxx:389
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition TSocket.cxx:522
void Print(const char *filename="") const override=0
This method must be overridden when a class wants to print itself.
return c1
Definition legend1.C:41
Fons Rademakers

Definition in file hserv.C.