Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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;
// }
{
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:45
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
@ kNoBlock
Definition TSystem.h:230
Basic string class.
Definition TString.h:139
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.
Author

Definition in file testTUDPSocket.C.