ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
testTUDPSocket.C
Go to the documentation of this file.
1 #include "TUDPSocket.h"
2 #include "TString.h"
3 
4 //
5 // As test echo server use udpserver.c in the same directory.
6 // To compile it do:
7 // clang udpserver.c -o udpserver
8 //
9 
10 // int createTServerSocket(){
11 // printf("testTSocket: Creating TSocket\n");
12 // TServerSocket * fServerSocket = new TServerSocket(1501, 0, 0, -1, "UDP");
13 //
14 // TMonitor *mon = new TMonitor;
15 //
16 // mon->Add(fServerSocket);
17 //
18 // TSocket *s0 = 0;
19 //
20 // // while (1) {
21 // char msgRaw[1024] = {0};
22 // TSocket *s;
23 //
24 // s = mon->Select();
25 //
26 // if (s->IsA() == TServerSocket::Class()) {
27 // if (!s0) {
28 // s0 = ((TServerSocket *)s)->Accept();
29 // s0->Send("go 0");
30 // mon->Add(s0);
31 // }
32 //
33 // if (s0) {
34 // mon->Remove(ss);
35 // ss->Close();
36 // }
37 // continue;
38 // }
39 //
40 // s->RecvRaw((void *) msgRaw, 1024);
41 // printf("Server Message Received %s\n", msgRaw);
42 // s->SendRaw(msgRaw, 1024);
43 //
44 // // }/* end of server infinite loop */
45 //
46 // s0->Close();
47 //
48 // return 1;
49 // }
50 
52 {
53  printf("testTSocket: Creating TUDPSocket\n");
54  TUDPSocket * fSocket = new TUDPSocket("localhost", 1500);
55 
56  if (!fSocket || !fSocket->IsValid()) {
57  Error("testTSocket","cannot connect to localhost");
58  return -1;
59  }
60 
61  TString msg = "testTSocket: Testing TSocket with UDP";
62 
63  printf("%s\n",msg.Data());
64 
65  if (fSocket->SendRaw(msg.Data(), msg.Length()) == -1) {
66  Error("testTSocket", "error sending command to host %s", fServer.GetHost());
67  return -1;
68  }
69 
70  char msgRaw[1024] = {0};
71 
72  fSocket->SetOption(kNoBlock, 1);
73  fSocket->Select();
74 
75  Int_t recvBytes = fSocket->RecvRaw(msgRaw, 1024);
76 
77  if (recvBytes == -1){
78  Error("testTSocket", "error receiving data from host %s", fServer.GetHost());
79  return -1;
80  }
81 
82  printf("Received Message: \n%s\n",msgRaw);
83 
84  return 1;
85 }
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TUDPSocket.cxx:591
Ssiz_t Length() const
Definition: TString.h:390
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
const char * Data() const
Definition: TString.h:349
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TUDPSocket.cxx:988
void Error(const char *location, const char *msgfmt,...)
virtual Bool_t IsValid() const
Definition: TUDPSocket.h:137
int testTUDPSocket()
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TUDPSocket.cxx:412
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TUDPSocket.cxx:872