]> www.hackdaworld.org Git - my-code/ivac.git/blob - receive.c
105a81c2e27a42279b79e223715598d39b6b8b30
[my-code/ivac.git] / receive.c
1 /* receive.c - receive from streaming 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 int main(int argc, char *argv[]) {
30   int receive_fd;
31   struct sockaddr_in target_addr;
32   int receive_bytes, write_bytes;
33
34   if(argc!=3) {
35     printf("usage: %s <target-ip> <port>\n",argv[0]);
36     exit(1);
37   }
38
39   if((receive_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
40     printf("can't open socket.\n");
41     exit(1);
42   }
43  
44   memset(&target_addr,0,sizeof(target_addr));
45   target_addr.sin_family=AF_INET;
46   target_addr.sin_port=htons(atoi(argv[2]));
47   target_addr.sin_addr.s_addr=inet_addr(argv[1]);
48
49   if(connect(receive_fd,(struct sockaddr *)&target_addr,sizeof(target_addr))==-1) {
50   printf("unable to connect.\n");
51   perror("connect");
52   exit(1);
53   }
54
55   receive_bytes=1;
56   while(receive_bytes>0) {
57         unsigned char buf[1000];
58         receive_bytes=recv(receive_fd,buf,sizeof(buf),0);
59         write_bytes=write(1,buf,receive_bytes);
60   }
61
62   close(receive_fd);
63   printf("connection closed ...\n");
64   printf("%d from %d total bytes written.\n",write_bytes,receive_bytes);
65   
66   return 0;
67 }