#include #include #include #include #include #include #include using namespace std; int hserv2_mod(void) { // Create canvas and pad to display the histogram TCanvas *c1 = new TCanvas("c1","Histogram received",200,10,600,480); c1->Draw(); // Open a server socket looking for connections on a named service or // on a specified port. TServerSocket *ss = new TServerSocket(9090, kTRUE); TMonitor *mon = new TMonitor; mon->Add(ss); TSocket *s0 = 0; // this is my histogram TH1F * my_h = 0; // # of updates int updates = 0; while (1) { TMessage *mess; TSocket *s = mon->Select(); if (s->IsA() == TServerSocket::Class()) { if (!s0) { s0 = ((TServerSocket *)s)->Accept(); s0->Send("go"); mon->Add(s0); cout << " New connection opened " << endl; } else printf("only accept one client connection\n"); continue; } s->Recv(mess); if(!mess) { cout << " *** Oops! Connection has been dropped " << endl; // connection has been dropped - do some cleanup mon->Remove(s0); s0->Close(); s0 = 0; if(my_h) { cout << " Deleting " << my_h->GetName() << endl; delete my_h; my_h = 0; } continue; } ++updates; cout << " # of updates = " << updates << endl; if (mess->What() == kMESS_OBJECT) { TH1F *h = (TH1F *)mess->ReadObject(mess->GetClass()); if(!my_h) { my_h = new TH1F("myname", h->GetTitle(), h->GetNbinsX(), h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax()); my_h->SetDirectory(0); } my_h->Reset(); my_h->Add(h); my_h->Draw(); c1->Modified(); c1->Update(); delete h; // delete histogram that has been read in } else { printf("*** Unexpected message ***\n"); } delete mess; } // we get here only if we break out of the loop s0->Close(); // close the socket return 0; }