24 #include <arpa/inet.h>
29 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
36 #define COMMUNICATION_PORT "2035" // the port on ZedBoard for communicating with XDAQ
37 #define STREAMING_PORT "2036" // the port on ZedBoard for streaming to XDAQ
38 #define DESTINATION_IP "192.168.133.5" // the IP for the destination of the datastream
39 #define DESTINATION_PORT 2039 // the port for the destination of the datastream
40 #define MAXBUFLEN 1492
44 void* get_in_addr(
struct sockaddr* sa)
46 if(sa->sa_family == AF_INET)
48 return &(((
struct sockaddr_in*)sa)->sin_addr);
51 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
55 int makeSocket(
const char* ip,
const char* port,
struct addrinfo*& )
58 struct addrinfo hints, *servinfo, *p;
67 memset(&hints, 0,
sizeof hints);
69 hints.ai_family = AF_INET;
70 hints.ai_socktype = SOCK_DGRAM;
72 hints.ai_flags = AI_PASSIVE;
74 if((rv = getaddrinfo(ip, port, &hints, &servinfo)) != 0)
76 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
81 for(p = servinfo; p != NULL; p = p->ai_next)
83 if((socketId = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
85 perror(
"listener: socket");
89 if(bind(socketId, p->ai_addr, p->ai_addrlen) == -1)
92 perror(
"listener: bind");
101 fprintf(stderr,
"listener: failed to bind socket\n");
104 freeaddrinfo(servinfo);
109 struct sockaddr_in setupSocketAddress(const char* ip,
unsigned int port)
113 struct sockaddr_in socketAddress;
114 socketAddress.sin_family = AF_INET;
115 socketAddress.sin_port = htons(port);
117 if(inet_aton(ip, &socketAddress.sin_addr) == 0)
119 std::cout <<
"FATAL: Invalid IP address " << ip << std::endl;
123 memset(&(socketAddress.sin_zero),
'\0', 8);
124 return socketAddress;
128 int send(
int toSocket,
struct sockaddr_in& toAddress,
const std::string& buffer)
130 std::cout <<
"Socket Descriptor #: " << toSocket <<
" ip: " << std::hex
131 << toAddress.sin_addr.s_addr << std::dec
132 <<
" port: " << ntohs(toAddress.sin_port) << std::endl;
137 (
struct sockaddr*)&(toAddress),
138 sizeof(sockaddr_in)) < (int)(buffer.size()))
140 std::cout <<
"Error writing buffer" << std::endl;
147 int receive(
int fromSocket,
struct sockaddr_in& fromAddress, std::string& buffer)
152 fd_set fileDescriptor;
153 FD_ZERO(&fileDescriptor);
154 FD_SET(fromSocket, &fileDescriptor);
155 select(fromSocket + 1, &fileDescriptor, 0, 0, &tv);
157 if(FD_ISSET(fromSocket, &fileDescriptor))
161 socklen_t addressLength =
sizeof(fromAddress);
163 buffer.resize(MAXBUFLEN);
164 if((nOfBytes = recvfrom(fromSocket,
168 (
struct sockaddr*)&fromAddress,
169 &addressLength)) == -1)
173 send(fromSocket, fromAddress, buffer);
174 buffer.resize(nOfBytes);
197 int communicationSocket = makeSocket(0, COMMUNICATION_PORT, p);
200 struct sockaddr_in messageSender;
202 std::string communicationBuffer;
208 std::cout <<
"Waiting for DAQ communication..." << std::endl;
209 bool triggered =
false;
210 const unsigned ndata = 2 * 64;
215 if(receive(communicationSocket, messageSender, communicationBuffer) >= 0)
217 std::cout <<
"Message received!!!" << std::endl;
218 std::cout << communicationBuffer << std::endl;
220 if(communicationBuffer ==
"START")
224 else if(communicationBuffer ==
"STOP")
226 std::cout << 4 * 66 * count << std::endl;
234 unsigned simdata[ndata + 5];
235 simdata[0] = 0xFFFFFFFF;
236 simdata[1] = 0xFFFFFFFF;
237 simdata[2] = 0xFFFFFFFF;
238 simdata[3] = 0xFFFFFFFF;
239 simdata[4] = 0xFFFFFFFF;
241 for(
unsigned i = 0; i < ndata; i++)
243 unsigned noise = rand() & 0x00FF00FF;
244 simdata[i + 5] = 0x00FF00FF + noise;
246 std::cout << std::hex << std::setw(8) << std::setfill(
'0') << simdata[i]
258 close(communicationSocket);