24 #include <arpa/inet.h>
29 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
39 #define COMMUNICATION_PORT "2035" // the port on ZedBoard for communicating with XDAQ
40 #define STREAMING_PORT "2036" // the port on ZedBoard for streaming to XDAQ
41 #define DESTINATION_IP "192.168.133.5" // the IP for the destination of the datastream
42 #define DESTINATION_PORT 2039 // the port for the destination of the datastream
43 #define MAXBUFLEN 1492
47 void* get_in_addr(
struct sockaddr* sa)
49 if(sa->sa_family == AF_INET)
51 return &(((
struct sockaddr_in*)sa)->sin_addr);
54 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
58 int makeSocket(
const char* ip,
const char* port,
struct addrinfo*& addressInfo)
61 struct addrinfo hints, *servinfo, *p;
65 struct sockaddr_storage their_addr;
68 char s[INET6_ADDRSTRLEN];
70 memset(&hints, 0,
sizeof hints);
72 hints.ai_family = AF_INET;
73 hints.ai_socktype = SOCK_DGRAM;
75 hints.ai_flags = AI_PASSIVE;
77 if((rv = getaddrinfo(ip, port, &hints, &servinfo)) != 0)
79 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
84 for(p = servinfo; p != NULL; p = p->ai_next)
86 if((socketId = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
88 perror(
"listener: socket");
92 if(bind(socketId, p->ai_addr, p->ai_addrlen) == -1)
95 perror(
"listener: bind");
104 fprintf(stderr,
"listener: failed to bind socket\n");
107 freeaddrinfo(servinfo);
112 struct sockaddr_in setupSocketAddress(const char* ip,
unsigned int port)
116 struct sockaddr_in socketAddress;
117 socketAddress.sin_family = AF_INET;
118 socketAddress.sin_port = htons(port);
120 if(inet_aton(ip, &socketAddress.sin_addr) == 0)
122 std::cout <<
"FATAL: Invalid IP address " << ip << std::endl;
126 memset(&(socketAddress.sin_zero),
'\0', 8);
127 return socketAddress;
131 int send(
int toSocket,
struct sockaddr_in& toAddress,
const std::string& buffer)
133 std::cout <<
"Socket Descriptor #: " << toSocket <<
" ip: " << std::hex
134 << toAddress.sin_addr.s_addr << std::dec
135 <<
" port: " << ntohs(toAddress.sin_port) << std::endl;
140 (
struct sockaddr*)&(toAddress),
141 sizeof(sockaddr_in)) < (int)(buffer.size()))
143 std::cout <<
"Error writing buffer" << std::endl;
150 int receive(
int fromSocket,
struct sockaddr_in& fromAddress, std::string& buffer)
155 fd_set fileDescriptor;
156 FD_ZERO(&fileDescriptor);
157 FD_SET(fromSocket, &fileDescriptor);
158 select(fromSocket + 1, &fileDescriptor, 0, 0, &tv);
160 if(FD_ISSET(fromSocket, &fileDescriptor))
164 socklen_t addressLength =
sizeof(fromAddress);
166 buffer.resize(MAXBUFLEN);
167 if((nOfBytes = recvfrom(fromSocket,
171 (
struct sockaddr*)&fromAddress,
172 &addressLength)) == -1)
176 send(fromSocket, fromAddress, buffer);
177 buffer.resize(nOfBytes);
199 struct addrinfo hints, *servinfo, *p;
201 int communicationSocket = makeSocket(0, COMMUNICATION_PORT, p);
202 int streamingSocket = makeSocket(0, STREAMING_PORT, p);
203 struct sockaddr_in streamingReceiver =
204 setupSocketAddress(DESTINATION_IP, DESTINATION_PORT);
205 struct sockaddr_in messageSender;
207 std::string communicationBuffer;
213 cout <<
"Waiting for DAQ communication..." << endl;
214 bool triggered =
false;
215 const unsigned ndata = 2 * 64;
220 if(receive(communicationSocket, messageSender, communicationBuffer) >= 0)
222 cout <<
"Message received!!!" << endl;
223 cout << communicationBuffer << endl;
225 if(communicationBuffer ==
"START")
229 else if(communicationBuffer ==
"STOP")
231 cout << 4 * 66 * count << endl;
239 unsigned simdata[ndata + 5];
240 simdata[0] = 0xFFFFFFFF;
241 simdata[1] = 0xFFFFFFFF;
242 simdata[2] = 0xFFFFFFFF;
243 simdata[3] = 0xFFFFFFFF;
244 simdata[4] = 0xFFFFFFFF;
246 for(
unsigned i = 0; i < ndata; i++)
248 unsigned noise = rand() & 0x00FF00FF;
249 simdata[i + 5] = 0x00FF00FF + noise;
251 std::cout << std::hex << std::setw(8) << std::setfill(
'0') << simdata[i]
263 close(communicationSocket);