DABC (Data Acquisition Backbone Core)  2.9.9
aquatest.c
Go to the documentation of this file.
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <arpa/inet.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <time.h>
12 
13 int main(int argc, char *argv[])
14 {
15  int listenfd = 0, connfd = 0;
16  struct sockaddr_in serv_addr;
17 
18  char* recvBuff = malloc(512*1024);
19  if (!recvBuff) return 1;
20 
21  int64_t header[2];
22  int64_t totalsz, totalbuf, totallost, lastcnt;
23 
24  time_t tm, lasttm;
25 
26  ssize_t sz;
27 
28  listenfd = socket(AF_INET, SOCK_STREAM, 0);
29  memset(&serv_addr, '0', sizeof(serv_addr));
30 
31  serv_addr.sin_family = AF_INET;
32  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
33  serv_addr.sin_port = htons(5000);
34 
35  bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
36 
37  listen(listenfd, 10);
38 
39  lastcnt = -1;
40  totalsz = 0; totalbuf = 0; totallost = 0;
41  lasttm = time(0);
42 
43 
44  while(1)
45  {
46  connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
47 
48  if (connfd <= 0) {
49  printf("Fail to accept connection\n");
50  close(listenfd);
51  free(recvBuff);
52  return 0;
53  }
54 
55  printf("Client connected\n");
56 
57  while (1) {
58  sz = recv(connfd, header, sizeof(header), MSG_WAITALL);
59  if (sz == 0) continue;
60  if (sz != sizeof(header)) {
61  printf("Error when receive header %lu res %ld\n", (long unsigned) sizeof(header), (long) sz);
62  close(listenfd);
63  free(recvBuff);
64  return 0;
65  }
66 
67  sz = recv(connfd, recvBuff, header[1], MSG_WAITALL);
68  if (sz != header[1]) {
69  printf("Error when receive buffer %ld res %ld\n", (long) header[1], (long) sz);
70  close(listenfd);
71  free(recvBuff);
72  return 0;
73  }
74 
75  // printf("get buffer %ld\n", header[1]);
76 
77  totalsz+=sz;
78  totalbuf++;
79  if ((lastcnt>0) && (header[0]-lastcnt>1)) totallost+=(header[0]-lastcnt-1);
80 
81  lastcnt = header[0];
82 
83  tm = time(0);
84  if (tm - lasttm > 3) {
85  printf("Bufs %6lld Size %8.3f MB Lost %3lld\n", (long long) totalbuf, totalsz*1e-6, (long long) totallost);
86  lasttm = tm;
87  }
88  }
89 
90  }
91 
92  free(recvBuff);
93  return 0;
94 }
int main(int argc, char *argv[])
Definition: aquatest.c:13