Rev 951 | Details | Compare with Previous | 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 | |||
1063 | tullio | 18 | /* |
19 | * This program is free software; you can redistribute it and/or modify |
||
20 | * it under the terms of the GNU General Public License as published by |
||
21 | * the Free Software Foundation; either version 2 of the License, or |
||
22 | * (at your option) any later version. |
||
23 | * |
||
24 | * This program is distributed in the hope that it will be useful, |
||
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
27 | * GNU General Public License for more details. |
||
28 | * |
||
29 | * You should have received a copy of the GNU General Public License |
||
30 | * along with this program; if not, write to the Free Software |
||
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
32 | * |
||
33 | */ |
||
34 | |||
951 | trimarchi | 35 | #include <netinet/in.h> |
36 | |||
37 | #include <sys/socket.h> |
||
38 | #include <netinet/in.h> |
||
39 | #include <arpa/inet.h> |
||
40 | #include <netdb.h> |
||
41 | #include <stdio.h> |
||
42 | #include <unistd.h>/* close() */ |
||
43 | #include <stdlib.h> |
||
44 | #include <signal.h> |
||
45 | #include <string.h> |
||
46 | |||
47 | #define SERVER_PORT 24000 |
||
48 | #define MAX_MSG 10000 |
||
49 | |||
50 | struct tests_udp_header { |
||
51 | char id[12]; |
||
52 | unsigned int pkt_number; |
||
53 | unsigned int size; |
||
54 | }; |
||
55 | |||
56 | FILE *output_file; |
||
57 | |||
58 | int miss; |
||
59 | |||
60 | void close_and_exit() |
||
61 | { |
||
62 | |||
63 | printf("Closing...\n"); |
||
64 | |||
65 | if (miss == 1) printf("Possible error receiving packets !\n"); |
||
66 | |||
67 | fclose(output_file); |
||
68 | |||
69 | exit(0); |
||
70 | |||
71 | } |
||
72 | |||
73 | int main(int argc, char *argv[]) |
||
74 | { |
||
75 | int sd, rc, n, cliLen,count; |
||
76 | struct sockaddr_in cliAddr, servAddr; |
||
77 | char msg[MAX_MSG]; |
||
78 | |||
79 | struct tests_udp_header *pkt_head = (struct tests_udp_header *)(msg); |
||
80 | |||
81 | if (argc < 2) { |
||
82 | printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]); |
||
83 | exit(1); |
||
84 | } |
||
85 | |||
86 | // socket creation |
||
87 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
||
88 | if(sd < 0) { |
||
89 | printf("%s: cannot open socket \n",argv[0]); |
||
90 | exit(1); |
||
91 | } |
||
92 | |||
93 | output_file = fopen(argv[1],"w+b"); |
||
94 | if (output_file == NULL) { |
||
95 | printf("%s: Cannot open the file\n",argv[0]); |
||
96 | exit(1); |
||
97 | } |
||
98 | |||
99 | // bind local server port |
||
100 | servAddr.sin_family = AF_INET; |
||
101 | |||
102 | servAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
||
103 | servAddr.sin_port = htons(SERVER_PORT); |
||
104 | |||
105 | rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr)); |
||
106 | if(rc < 0) { |
||
107 | printf("%s: cannot bind port number %d \n", |
||
108 | argv[0], SERVER_PORT); |
||
109 | exit(1); |
||
110 | } |
||
111 | |||
112 | signal(SIGINT, close_and_exit); |
||
113 | |||
114 | count = 1; |
||
115 | miss = 0; |
||
116 | |||
117 | while(1) { |
||
118 | |||
119 | printf("Wait packet...\n"); |
||
120 | |||
121 | // receive message |
||
122 | cliLen = sizeof(cliAddr); |
||
123 | n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen); |
||
124 | |||
125 | //if (strncmp(pkt_head->id,"TESTS",6)) continue; |
||
126 | |||
127 | printf("Received %d, length %d(%d)\n", |
||
128 | pkt_head->pkt_number, n, pkt_head->size); |
||
129 | |||
130 | if (pkt_head->pkt_number != count) { |
||
131 | printf("Miss Packet !!!\n"); |
||
132 | miss = 1; |
||
133 | } |
||
134 | |||
135 | if (n > 0) { |
||
136 | |||
137 | count++; |
||
138 | |||
139 | fwrite((void *)(msg+sizeof(struct tests_udp_header)),n-sizeof(struct tests_udp_header),1,output_file); |
||
140 | |||
141 | } |
||
142 | |||
143 | } |
||
144 | |||
145 | fclose(output_file); |
||
146 | |||
147 | return 0; |
||
148 | |||
149 | } |
||
150 |