File: udp_recv.c

package info (click to toggle)
dynamips 0.2.7-0.2.8RC2-2
  • links: PTS
  • area: non-free
  • in suites: lenny
  • size: 3,840 kB
  • ctags: 9,893
  • sloc: ansic: 69,846; makefile: 239; sh: 124; perl: 20
file content (43 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>

#include "utils.h"
#include "net.h"

#define MAX_PKT_SIZE  2048

int main(int argc,char *argv[])
{
   char pkt[MAX_PKT_SIZE];
   ssize_t pkt_size;
   int sck = -1;
   FILE *fd;

   /* Wait connection */
   if (ip_listen(NULL,atoi(argv[2]),SOCK_DGRAM,1,&sck) < 1) {
      perror("ip_listen");
      exit(EXIT_FAILURE);
   }

   /* Receive packet and store it */
   if ((pkt_size = recvfrom(sck,pkt,sizeof(pkt),0,NULL,NULL)) < 0) {
      perror("recvfrom");
      exit(EXIT_FAILURE);
   }

   if (!(fd = fopen(argv[1],"w"))) {
      perror("fopen");
      exit(EXIT_FAILURE);
   }

   fwrite(pkt,1,pkt_size,fd);
   fclose(fd);
   close(sck);
   return(0);
}