ROOT logo

From $ROOTSYS/tutorials/net/testTUDPSocket.C

#include "TUDPSocket.h"
#include "TString.h"

//
// As test echo server use udpserver.c in the same directory.
// To compile it do:
//    clang udpserver.c -o udpserver
//

// int createTServerSocket(){
//    printf("testTSocket: Creating TSocket\n");
//    TServerSocket * fServerSocket = new TServerSocket(1501, 0, 0, -1, "UDP");
//
//    TMonitor *mon = new TMonitor;
//
//    mon->Add(fServerSocket);
//
//    TSocket *s0 = 0;
//
//  //  while (1) {
//       char msgRaw[1024] = {0};
//       TSocket  *s;
//
//       s = mon->Select();
//
//       if (s->IsA() == TServerSocket::Class()) {
//          if (!s0) {
//             s0 = ((TServerSocket *)s)->Accept();
//             s0->Send("go 0");
//             mon->Add(s0);
//          }
//
//          if (s0) {
//             mon->Remove(ss);
//             ss->Close();
//          }
//          continue;
//       }
//
//       s->RecvRaw((void *) msgRaw, 1024);
//       printf("Server Message Received %s\n", msgRaw);
//       s->SendRaw(msgRaw, 1024);
//
// // }/* end of server infinite loop */
//
//    s0->Close();
//
//    return 1;
// }

int testTUDPSocket()
{
   printf("testTSocket: Creating TUDPSocket\n");
   TUDPSocket * fSocket = new TUDPSocket("localhost", 1500);

   if (!fSocket || !fSocket->IsValid()) {
      Error("testTSocket","cannot connect to localhost");
      return -1;
   }

   TString msg = "testTSocket: Testing TSocket with UDP";

   printf("%s\n",msg.Data());

   if (fSocket->SendRaw(msg.Data(), msg.Length()) == -1) {
      Error("testTSocket", "error sending command to host %s", fServer.GetHost());
      return -1;
   }

   char msgRaw[1024] = {0};

   fSocket->SetOption(kNoBlock, 1);
   fSocket->Select();

   Int_t recvBytes = fSocket->RecvRaw(msgRaw, 1024);

   if (recvBytes == -1){
      Error("testTSocket", "error receiving data from host %s", fServer.GetHost());
      return -1;
   }

   printf("Received Message: \n%s\n",msgRaw);

   return 1;
}
 testTUDPSocket.C:1
 testTUDPSocket.C:2
 testTUDPSocket.C:3
 testTUDPSocket.C:4
 testTUDPSocket.C:5
 testTUDPSocket.C:6
 testTUDPSocket.C:7
 testTUDPSocket.C:8
 testTUDPSocket.C:9
 testTUDPSocket.C:10
 testTUDPSocket.C:11
 testTUDPSocket.C:12
 testTUDPSocket.C:13
 testTUDPSocket.C:14
 testTUDPSocket.C:15
 testTUDPSocket.C:16
 testTUDPSocket.C:17
 testTUDPSocket.C:18
 testTUDPSocket.C:19
 testTUDPSocket.C:20
 testTUDPSocket.C:21
 testTUDPSocket.C:22
 testTUDPSocket.C:23
 testTUDPSocket.C:24
 testTUDPSocket.C:25
 testTUDPSocket.C:26
 testTUDPSocket.C:27
 testTUDPSocket.C:28
 testTUDPSocket.C:29
 testTUDPSocket.C:30
 testTUDPSocket.C:31
 testTUDPSocket.C:32
 testTUDPSocket.C:33
 testTUDPSocket.C:34
 testTUDPSocket.C:35
 testTUDPSocket.C:36
 testTUDPSocket.C:37
 testTUDPSocket.C:38
 testTUDPSocket.C:39
 testTUDPSocket.C:40
 testTUDPSocket.C:41
 testTUDPSocket.C:42
 testTUDPSocket.C:43
 testTUDPSocket.C:44
 testTUDPSocket.C:45
 testTUDPSocket.C:46
 testTUDPSocket.C:47
 testTUDPSocket.C:48
 testTUDPSocket.C:49
 testTUDPSocket.C:50
 testTUDPSocket.C:51
 testTUDPSocket.C:52
 testTUDPSocket.C:53
 testTUDPSocket.C:54
 testTUDPSocket.C:55
 testTUDPSocket.C:56
 testTUDPSocket.C:57
 testTUDPSocket.C:58
 testTUDPSocket.C:59
 testTUDPSocket.C:60
 testTUDPSocket.C:61
 testTUDPSocket.C:62
 testTUDPSocket.C:63
 testTUDPSocket.C:64
 testTUDPSocket.C:65
 testTUDPSocket.C:66
 testTUDPSocket.C:67
 testTUDPSocket.C:68
 testTUDPSocket.C:69
 testTUDPSocket.C:70
 testTUDPSocket.C:71
 testTUDPSocket.C:72
 testTUDPSocket.C:73
 testTUDPSocket.C:74
 testTUDPSocket.C:75
 testTUDPSocket.C:76
 testTUDPSocket.C:77
 testTUDPSocket.C:78
 testTUDPSocket.C:79
 testTUDPSocket.C:80
 testTUDPSocket.C:81
 testTUDPSocket.C:82
 testTUDPSocket.C:83
 testTUDPSocket.C:84
 testTUDPSocket.C:85
 testTUDPSocket.C:86