]> www.hackdaworld.org Git - my-code/ivac.git/blob - old/udp_rcv.c
introduced special network return codes, added {de,}select, connect, close, set functions
[my-code/ivac.git] / old / udp_rcv.c
1 /* dgramrcv.c - receive from udp server
2  *
3  * author: hackbard
4  *
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 /* memset */
11 #include <string.h>
12
13 /* socket and bind stuff */
14 #include <sys/types.h>
15 #include <sys/socket.h>
16
17 /* sockkaddr_in */
18 #include <netinet/in.h>
19
20 /* inet_ntoa ... */
21 #include <arpa/inet.h>
22
23 /* errno stuff ... */
24 #include <errno.h>
25
26 /* read, close */
27 #include <unistd.h>
28
29 #define MAX_BUF 1472
30
31 int main(int argc, char *argv[]) {
32   int receive_fd;
33   int opt;
34   struct sockaddr_in local_addr,target_addr;
35   int receive_bytes, write_bytes;
36   socklen_t target_addr_len;
37   unsigned char buf[MAX_BUF];
38
39   if(argc!=2) {
40     printf("usage: %s <port>\n",argv[0]);
41     exit(1);
42   }
43
44   if((receive_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1) {
45     printf("can't open socket.\n");
46     exit(1);
47   }
48
49   opt=1;
50   setsockopt(receive_fd,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt));
51
52   local_addr.sin_family=AF_INET;
53   local_addr.sin_port=htons(atoi(argv[1]));
54   local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
55   bind(receive_fd,(const struct sockaddr *)&local_addr,sizeof(local_addr));
56  
57   memset(&target_addr,0,sizeof(target_addr));
58  /*
59   target_addr.sin_family=AF_INET;
60   target_addr.sin_port=htons(atoi(argv[1]));
61   target_addr.sin_addr.s_addr=inet_addr(argv[1]);
62   target_addr_len=sizeof(target_addr);
63  */
64
65   receive_bytes=1;
66   while(receive_bytes>0) {
67         receive_bytes=recvfrom(receive_fd,buf,sizeof(buf),0,(struct sockaddr *)&target_addr,&target_addr_len);
68         write_bytes=write(1,buf,receive_bytes);
69   }
70
71   close(receive_fd);
72   printf("connection closed ...\n");
73   printf("%d from %d total bytes written.\n",write_bytes,receive_bytes);
74   
75   return 0;
76 }