hserv.C

Go to the documentation of this file.
00001 void hserv() {
00002    // Server program which waits for two clients to connect. It then monitors
00003    // the sockets and displays the objects it receives. To see how to
00004    // make a non-blocking server see the script hserv2.C.
00005    //
00006    // To run this demo do the following:
00007    //   - Open three windows
00008    //   - Start ROOT in all three windows
00009    //   - Execute in the first window: .x hserv.C
00010    //   - Execute in the second and third windows: .x hclient.C
00011    //Author: Fons Rademakers
00012    
00013    // Open a server socket looking for connections on a named service or
00014    // on a specified port.
00015    //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
00016    TServerSocket *ss = new TServerSocket(9090, kTRUE);
00017 
00018    // Accept a connection and return a full-duplex communication socket.
00019    TSocket *s0 = ss->Accept();
00020    TSocket *s1 = ss->Accept();
00021 
00022    // tell the clients to start
00023    s0->Send("go 0");
00024    s1->Send("go 1");
00025 
00026    // Close the server socket (unless we will use it later to wait for
00027    // another connection).
00028    ss->Close();
00029 
00030    // Check some options of socket 0.
00031    int val;
00032    s0->GetOption(kSendBuffer, val);
00033    printf("sendbuffer size: %d\n", val);
00034    s0->GetOption(kRecvBuffer, val);
00035    printf("recvbuffer size: %d\n", val);
00036 
00037    // Get the remote addresses (informational only).
00038    TInetAddress adr = s0->GetInetAddress();
00039    adr.Print();
00040    adr = s1->GetInetAddress();
00041    adr.Print();
00042 
00043    // Create canvas and pads to display the histograms
00044    TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
00045    TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
00046    TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
00047    pad1->Draw();
00048    pad2->Draw();
00049 
00050    TMonitor *mon = new TMonitor;
00051 
00052    mon->Add(s0);
00053    mon->Add(s1);
00054 
00055    while (1) {
00056       TMessage *mess;
00057       TSocket  *s;
00058 
00059       s = mon->Select();
00060 
00061       s->Recv(mess);
00062 
00063       if (mess->What() == kMESS_STRING) {
00064          char str[64];
00065          mess->ReadString(str, 64);
00066          printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
00067          mon->Remove(s);
00068          if (mon->GetActive() == 0) {
00069             printf("No more active clients... stopping\n");
00070             break;
00071          }
00072       } else if (mess->What() == kMESS_OBJECT) {
00073          //printf("got object of class: %s\n", mess->GetClass()->GetName());
00074          TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
00075          if (h) {
00076             if (s == s0)
00077                pad1->cd();
00078             else
00079                pad2->cd();
00080             h->Print();
00081             h->DrawCopy();  //draw a copy of the histogram, not the histo itself
00082             c1->Modified();
00083             c1->Update();
00084             delete h;       // delete histogram
00085          }
00086       } else {
00087          printf("*** Unexpected message ***\n");
00088       }
00089 
00090       delete mess;
00091    }
00092 
00093    printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
00094           s0->GetBytesSent());
00095    printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
00096           s1->GetBytesSent());
00097 
00098    // Close the socket.
00099    s0->Close();
00100    s1->Close();
00101 }

Generated on Tue Jul 5 15:44:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1