From $ROOTSYS/tutorials/net/hclientbonj.C

#include "TBenchmark.h"
#include "TList.h"
#include "TInetAddress.h"
#include "TSocket.h"
#include "TMessage.h"
#include "TH1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TBonjourBrowser.h"
#include "TBonjourResolver.h"
#include "TBonjourRecord.h"


static Bool_t gEvo = kFALSE;

void ConnectToServer(const TInetAddress *hostb, Int_t port)
{
   // Called by the Bonjour resolver with the host and port to which
   // we can connect.

   // Connect only once...
   TBonjourResolver *resolver = (TBonjourResolver*) gTQSender;
   TInetAddress host = *hostb; 
   delete resolver;

   printf("ConnectToServer: host = %s, port = %d\n", host.GetHostName(), port);

   //--- Here starts original hclient.C code ---

   // Open connection to server
   TSocket *sock = new TSocket(host.GetHostName(), port);

   // Wait till we get the start message
   char str[32];
   sock->Recv(str, 32);

   // server tells us who we are
   int idx = !strcmp(str, "go 0") ? 0 : 1;

   Float_t messlen  = 0;
   Float_t cmesslen = 0;
   if (idx == 1)
      sock->SetCompressionLevel(1);

   TH1 *hpx;
   if (idx == 0) {
      // Create the histogram
      hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
      hpx->SetFillColor(48);  // set nice fillcolor
   } else {
      hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
   }

   TMessage::EnableSchemaEvolutionForAll(gEvo);
   TMessage mess(kMESS_OBJECT);
   //TMessage mess(kMESS_OBJECT | kMESS_ACK);

   // Fill histogram randomly
   gRandom->SetSeed();
   Float_t px, py;
   const int kUPDATE = 1000;
   for (int i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      if (idx == 0)
         hpx->Fill(px);
      else
         hpx->Fill(px,py);
      if (i && (i%kUPDATE) == 0) {
         mess.Reset();              // re-use TMessage object
         mess.WriteObject(hpx);     // write object in message buffer
         sock->Send(mess);          // send message
         messlen  += mess.Length();
         cmesslen += mess.CompLength();
      }
   }
   sock->Send("Finished");          // tell server we are finished

   if (cmesslen > 0)
      printf("Average compression ratio: %g\n", messlen/cmesslen);

   gBenchmark->Show("hclient");

   // Close the socket
   sock->Close();
}

void UpdateBonjourRecords(TList *records)
{
   // Browse for Bonjour record of type "_hserv2._tcp." in domain "local.".
   // When found, create Bonjour resolver to get host and port of this record.

   static Bool_t resolved = kFALSE;

   // we can be called multiple times whenever a new server appears
   printf("UpdateBonjourRecords (resolved = %s)\n", resolved ? "kTRUE" : "kFALSE");

   if (resolved) return;

   // Look for _hserv2._tcp. in local. domain and try to resolve it
   TBonjourRecord *rec;
   TIter next(records);
   while ((rec = (TBonjourRecord*) next())) {
      if (!strcmp(rec->GetRegisteredType(), "_hserv2._tcp.") &&
          !strcmp(rec->GetReplyDomain(), "local.")) {
         rec->Print();
         TBonjourResolver *resolver = new TBonjourResolver;
         resolver->Connect("RecordResolved(TInetAddress*,Int_t)", 0, 0,
                           "ConnectToServer(TInetAddress*,Int_t)");
         resolver->ResolveBonjourRecord(*rec);
         resolved = kTRUE;
      }
   }
}

void hclientbonj(Bool_t evol=kFALSE)
{
   // Client program which creates and fills a histogram. Every 1000 fills
   // the histogram is send to the server which displays the histogram.
   //
   // To run this demo do the following:
   //   - Open three windows
   //   - Start ROOT in all three windows
   //   - Execute in the first window: .x hserv.C (or hserv2.C)
   //   - Execute in the second and third windows: .x hclient.C
   // If you want to run the hserv.C on a different host, just change
   // "localhost" in the TSocket ctor below to the desired hostname.
   //
   // The script argument "evol" can be used when using a modified version
   // of the script where the clients and server are on systems with
   // different versions of ROOT. When evol is set to kTRUE the socket will
   // support automatic schema evolution between the client and the server.
   //
   //Author: Fons Rademakers

   gEvo = evol;

   gBenchmark->Start("hclient");

   TBonjourBrowser *browser = new TBonjourBrowser;
   browser->Connect("CurrentBonjourRecordsChanged(TList*)", 0, 0,
                    "UpdateBonjourRecords(TList*)");
   browser->BrowseForServiceType("_hserv2._tcp");
}
 hclientbonj.C:1
 hclientbonj.C:2
 hclientbonj.C:3
 hclientbonj.C:4
 hclientbonj.C:5
 hclientbonj.C:6
 hclientbonj.C:7
 hclientbonj.C:8
 hclientbonj.C:9
 hclientbonj.C:10
 hclientbonj.C:11
 hclientbonj.C:12
 hclientbonj.C:13
 hclientbonj.C:14
 hclientbonj.C:15
 hclientbonj.C:16
 hclientbonj.C:17
 hclientbonj.C:18
 hclientbonj.C:19
 hclientbonj.C:20
 hclientbonj.C:21
 hclientbonj.C:22
 hclientbonj.C:23
 hclientbonj.C:24
 hclientbonj.C:25
 hclientbonj.C:26
 hclientbonj.C:27
 hclientbonj.C:28
 hclientbonj.C:29
 hclientbonj.C:30
 hclientbonj.C:31
 hclientbonj.C:32
 hclientbonj.C:33
 hclientbonj.C:34
 hclientbonj.C:35
 hclientbonj.C:36
 hclientbonj.C:37
 hclientbonj.C:38
 hclientbonj.C:39
 hclientbonj.C:40
 hclientbonj.C:41
 hclientbonj.C:42
 hclientbonj.C:43
 hclientbonj.C:44
 hclientbonj.C:45
 hclientbonj.C:46
 hclientbonj.C:47
 hclientbonj.C:48
 hclientbonj.C:49
 hclientbonj.C:50
 hclientbonj.C:51
 hclientbonj.C:52
 hclientbonj.C:53
 hclientbonj.C:54
 hclientbonj.C:55
 hclientbonj.C:56
 hclientbonj.C:57
 hclientbonj.C:58
 hclientbonj.C:59
 hclientbonj.C:60
 hclientbonj.C:61
 hclientbonj.C:62
 hclientbonj.C:63
 hclientbonj.C:64
 hclientbonj.C:65
 hclientbonj.C:66
 hclientbonj.C:67
 hclientbonj.C:68
 hclientbonj.C:69
 hclientbonj.C:70
 hclientbonj.C:71
 hclientbonj.C:72
 hclientbonj.C:73
 hclientbonj.C:74
 hclientbonj.C:75
 hclientbonj.C:76
 hclientbonj.C:77
 hclientbonj.C:78
 hclientbonj.C:79
 hclientbonj.C:80
 hclientbonj.C:81
 hclientbonj.C:82
 hclientbonj.C:83
 hclientbonj.C:84
 hclientbonj.C:85
 hclientbonj.C:86
 hclientbonj.C:87
 hclientbonj.C:88
 hclientbonj.C:89
 hclientbonj.C:90
 hclientbonj.C:91
 hclientbonj.C:92
 hclientbonj.C:93
 hclientbonj.C:94
 hclientbonj.C:95
 hclientbonj.C:96
 hclientbonj.C:97
 hclientbonj.C:98
 hclientbonj.C:99
 hclientbonj.C:100
 hclientbonj.C:101
 hclientbonj.C:102
 hclientbonj.C:103
 hclientbonj.C:104
 hclientbonj.C:105
 hclientbonj.C:106
 hclientbonj.C:107
 hclientbonj.C:108
 hclientbonj.C:109
 hclientbonj.C:110
 hclientbonj.C:111
 hclientbonj.C:112
 hclientbonj.C:113
 hclientbonj.C:114
 hclientbonj.C:115
 hclientbonj.C:116
 hclientbonj.C:117
 hclientbonj.C:118
 hclientbonj.C:119
 hclientbonj.C:120
 hclientbonj.C:121
 hclientbonj.C:122
 hclientbonj.C:123
 hclientbonj.C:124
 hclientbonj.C:125
 hclientbonj.C:126
 hclientbonj.C:127
 hclientbonj.C:128
 hclientbonj.C:129
 hclientbonj.C:130
 hclientbonj.C:131
 hclientbonj.C:132
 hclientbonj.C:133
 hclientbonj.C:134
 hclientbonj.C:135
 hclientbonj.C:136
 hclientbonj.C:137
 hclientbonj.C:138
 hclientbonj.C:139
 hclientbonj.C:140
 hclientbonj.C:141
 hclientbonj.C:142
 hclientbonj.C:143
 hclientbonj.C:144