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