Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
testTUDPSocket.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_net
3/// As test echo server use udpserver.c in the same directory.
4/// To compile it do:
5///
6/// clang udpserver.c -o udpserver
7///
8/// \macro_code
9///
10/// \author
11
12#include "TUDPSocket.h"
13#include "TString.h"
14
15
16// int createTServerSocket(){
17// printf("testTSocket: Creating TSocket\n");
18// TServerSocket * fServerSocket = new TServerSocket(1501, 0, 0, -1, "UDP");
19//
20// TMonitor *mon = new TMonitor;
21//
22// mon->Add(fServerSocket);
23//
24// TSocket *s0 = 0;
25//
26// // while (1) {
27// char msgRaw[1024] = {0};
28// TSocket *s;
29//
30// s = mon->Select();
31//
32// if (s->IsA() == TServerSocket::Class()) {
33// if (!s0) {
34// s0 = ((TServerSocket *)s)->Accept();
35// s0->Send("go 0");
36// mon->Add(s0);
37// }
38//
39// if (s0) {
40// mon->Remove(ss);
41// ss->Close();
42// }
43// continue;
44// }
45//
46// s->RecvRaw((void *) msgRaw, 1024);
47// printf("Server Message Received %s\n", msgRaw);
48// s->SendRaw(msgRaw, 1024);
49//
50// // }/* end of server infinite loop */
51//
52// s0->Close();
53//
54// return 1;
55// }
56
57int testTUDPSocket()
58{
59 printf("testTSocket: Creating TUDPSocket\n");
60 TUDPSocket * fSocket = new TUDPSocket("localhost", 1500);
61
62 if (!fSocket || !fSocket->IsValid()) {
63 Error("testTSocket","cannot connect to localhost");
64 return -1;
65 }
66
67 TString msg = "testTSocket: Testing TSocket with UDP";
68
69 printf("%s\n",msg.Data());
70
71 if (fSocket->SendRaw(msg.Data(), msg.Length()) == -1) {
72 Error("testTSocket", "error sending command to host %s", fServer.GetHost());
73 return -1;
74 }
75
76 char msgRaw[1024] = {0};
77
78 fSocket->SetOption(kNoBlock, 1);
79 fSocket->Select();
80
81 Int_t recvBytes = fSocket->RecvRaw(msgRaw, 1024);
82
83 if (recvBytes == -1){
84 Error("testTSocket", "error receiving data from host %s", fServer.GetHost());
85 return -1;
86 }
87
88 printf("Received Message: \n%s\n",msgRaw);
89
90 return 1;
91}
int Int_t
Definition RtypesCore.h:45
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
@ kNoBlock
Definition TSystem.h:222
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
const char * Data() const
Definition TString.h:380
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
virtual Bool_t IsValid() const
Definition TUDPSocket.h:119
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.