Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1624 | giacomo | 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 20000 |
||
31 | #define MAX_MSG 10000 |
||
32 | |||
33 | FILE *output_file; |
||
34 | |||
35 | int miss; |
||
36 | |||
37 | void close_and_exit() |
||
38 | { |
||
39 | |||
40 | printf("Closing...\n"); |
||
41 | |||
42 | if (miss == 1) printf("Possible error receiving packets !\n"); |
||
43 | |||
44 | fclose(output_file); |
||
45 | |||
46 | exit(0); |
||
47 | |||
48 | } |
||
49 | |||
50 | int main(int argc, char *argv[]) |
||
51 | { |
||
52 | int sd, rc, n, cliLen,count; |
||
53 | struct sockaddr_in cliAddr, servAddr; |
||
54 | char msg[MAX_MSG]; |
||
55 | |||
56 | if (argc < 2) { |
||
57 | printf("%s: Enter the output file name [%s filename]\n",argv[0],argv[0]); |
||
58 | exit(1); |
||
59 | } |
||
60 | |||
61 | // socket creation |
||
62 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
||
63 | if(sd < 0) { |
||
64 | printf("%s: cannot open socket \n",argv[0]); |
||
65 | exit(1); |
||
66 | } |
||
67 | |||
68 | output_file = fopen(argv[1],"w+b"); |
||
69 | if (output_file == NULL) { |
||
70 | printf("%s: Cannot open the file\n",argv[0]); |
||
71 | exit(1); |
||
72 | } |
||
73 | |||
74 | // bind local server port |
||
75 | servAddr.sin_family = AF_INET; |
||
76 | |||
77 | servAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
||
78 | servAddr.sin_port = htons(SERVER_PORT); |
||
79 | |||
80 | rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr)); |
||
81 | if(rc < 0) { |
||
82 | printf("%s: cannot bind port number %d \n", |
||
83 | argv[0], SERVER_PORT); |
||
84 | exit(1); |
||
85 | } |
||
86 | |||
87 | signal(SIGINT, close_and_exit); |
||
88 | |||
89 | count = 1; |
||
90 | miss = 0; |
||
91 | |||
92 | while(1) { |
||
93 | |||
94 | printf("Wait packet...\n"); |
||
95 | |||
96 | // receive message |
||
97 | cliLen = sizeof(cliAddr); |
||
98 | n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen); |
||
99 | |||
100 | if (n > 0) { |
||
101 | |||
102 | count++; |
||
103 | |||
104 | fwrite((void *)(msg),n,1,output_file); |
||
105 | |||
106 | } |
||
107 | |||
108 | } |
||
109 | |||
110 | fclose(output_file); |
||
111 | |||
112 | return 0; |
||
113 | |||
114 | } |
||
115 |