Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
951 trimarchi 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *    Giacomo Guidi      <giacomo@gandalf.sssup.it>
10
 *
11
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
12
 *
13
 * http://www.sssup.it
14
 * http://retis.sssup.it
15
 * http://shark.sssup.it
16
 */
17
 
18
#include <netinet/in.h>
19
 
20
#include <sys/socket.h>
21
#include <netinet/in.h>
22
#include <arpa/inet.h>
23
#include <netdb.h>
24
#include <stdio.h>
25
#include <unistd.h>/* close() */
26
#include <stdlib.h>
27
#include <signal.h>
28
#include <string.h>
29
 
30
#define SERVER_PORT 24000 
31
#define MAX_MSG 10000
32
 
33
struct tests_udp_header {
34
  char id[12];
35
  unsigned int pkt_number;
36
  unsigned int size;
37
};
38
 
39
FILE *output_file;
40
 
41
int miss;
42
 
43
void close_and_exit()
44
{
45
 
46
  printf("Closing...\n");
47
 
48
  if (miss == 1) printf("Possible error receiving packets !\n");
49
 
50
  fclose(output_file);
51
 
52
  exit(0);
53
 
54
}
55
 
56
int main(int argc, char *argv[])
57
{
58
  int sd, rc, n, cliLen,count;
59
  struct sockaddr_in cliAddr, servAddr;
60
  char msg[MAX_MSG];
61
 
62
  struct tests_udp_header *pkt_head = (struct tests_udp_header *)(msg);
63
 
64
  if (argc < 2) {
65
    printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]);
66
    exit(1);
67
  }
68
 
69
  // socket creation
70
  sd = socket(AF_INET, SOCK_DGRAM, 0);
71
  if(sd < 0) {
72
    printf("%s: cannot open socket \n",argv[0]);
73
    exit(1);
74
  }
75
 
76
  output_file = fopen(argv[1],"w+b");
77
  if (output_file == NULL) {
78
    printf("%s: Cannot open the file\n",argv[0]);
79
    exit(1);
80
  }
81
 
82
  // bind local server port
83
  servAddr.sin_family = AF_INET;
84
 
85
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
86
  servAddr.sin_port = htons(SERVER_PORT);
87
 
88
  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
89
  if(rc < 0) {
90
    printf("%s: cannot bind port number %d \n",
91
           argv[0], SERVER_PORT);
92
    exit(1);
93
  }
94
 
95
  signal(SIGINT, close_and_exit);
96
 
97
  count = 1;
98
  miss = 0;
99
 
100
  while(1) {
101
 
102
    printf("Wait packet...\n");
103
 
104
    // receive message
105
    cliLen = sizeof(cliAddr);
106
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);
107
 
108
    //if (strncmp(pkt_head->id,"TESTS",6)) continue;
109
 
110
    printf("Received %d, length %d(%d)\n",
111
                pkt_head->pkt_number, n, pkt_head->size);
112
 
113
    if (pkt_head->pkt_number != count) {
114
      printf("Miss Packet !!!\n");
115
      miss = 1;
116
    }
117
 
118
    if (n > 0) {
119
 
120
      count++;
121
 
122
      fwrite((void *)(msg+sizeof(struct tests_udp_header)),n-sizeof(struct tests_udp_header),1,output_file);
123
 
124
    }
125
 
126
  }
127
 
128
  fclose(output_file);
129
 
130
  return 0;
131
 
132
}
133