Loading [MathJax]/jax/input/TeX/config.js
Logo ROOT  
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
testTUDPSocket.C File Reference

Detailed Description

As test echo server use udpserver.c in the same directory.

To compile it do:

    clang udpserver.c -o udpserver
#include "TUDPSocket.h"
#include "TString.h"
// 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;
}
int Int_t
Definition: RtypesCore.h:43
void Error(const char *location, const char *msgfmt,...)
@ kNoBlock
Definition: TSystem.h:221
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TUDPSocket.cxx:867
virtual Bool_t IsValid() const
Definition: TUDPSocket.h:119
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TUDPSocket.cxx:983
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TUDPSocket.cxx:407
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TUDPSocket.cxx:586
Author

Definition in file testTUDPSocket.C.