15 int main(int argc,char **argv);
18 static int bytes_received=0;
19 static char title[100];
22 static void usage(s) char *s;{
23 printf("usage: %s [-ms] host file [dest_file]\n",s);
28 int main(argc,argv) int argc; char** argv; {
30 int file_size,file_packets,size,x,y;
32 if( argc < 3 || argc > 5 ) usage(argv[0]);
34 if( argv[1][0]=='-' ){
36 set_cli_recv_timeout( atoi(argv[1]+1) );
37 printf("timeout set to %d ms\n",atoi(argv[1]+1));
38 for( i=1; i<argc-1; i++ ) argv[i]=argv[i+1];
52 printf("ask for %s:%s\n",host,file);
54 sock = sock_open( SOCK_DGRAM, "udp", host, NULL, PORT, AS_CLIENT );
55 if( sock<0 ) exit(10);
56 m.req = htonl( RQ_FILE_SIZE );
57 strcpy( m.data, file );
58 sock_cli_crc_send( sock, &m, HEAD+strlen(m.data)+1 );
59 size = sock_cli_crc_recv( sock, &m, sizeof(m) );
63 file_size = ntohl(*(int *)(m.data));
65 printf("file %s:%s not found\n",host,file),exit(0);
68 printf("error in packet\n");
70 printf("timed out\n");
74 fdest = fopen(dest,"wb");
76 printf("can't open file %s for writing\n",dest);
79 file_packets = (file_size+DATA_PIECE-1)/DATA_PIECE;
83 for(i=0;i*i<file_packets;i++);
89 while(x*y>file_packets) y--;
90 if(x*y<file_packets) y++;
94 sprintf(title,"%s:%s(%d b,pkt=%d b)",host,file,file_size,DATA_PIECE);
96 graph_board(file_packets);
99 extern void *calloc();
100 char *tab=calloc(1,file_packets);
101 while( !get_part( tab , file_packets ) );
108 printf("received %d bytes for file %s size %d\n",
109 bytes_received,file,file_size
117 int get_part( file_table , file_packets ) char *file_table; int file_packets; {
120 int skip_dupls=0,after_skip=0;
122 for( ; i<file_packets; i++ )
123 if( file_table[i]=='\0' )
128 if( i == file_packets ) return 1;
130 for( ; i<file_packets; i++ )
131 if( file_table[i]!='\0' )
138 while( i < file_packets ){
140 for( ; i<file_packets; i++ )
141 if( file_table[i]=='\0' )
146 if( i==file_packets ) break;
148 for( ; i<file_packets; i++ )
149 if( file_table[i]!='\0' )
154 /* if now we add skip_dupls & after_skip we will have duplicates */
155 /* but maybe with lower cost ! */
157 if( skip_dupls < after_skip )
158 cnt = skip_dupls + after_skip ;
164 printf("ask for %d packets, seek from %d file %s:%s\n",cnt,pos,host,file);
166 sock = sock_open(SOCK_DGRAM,"udp",host,NULL,PORT,AS_CLIENT);
167 if( sock<0 ) exit(10);
169 m.req = htonl( RQ_FILE );
170 ((int *)m.data)[0] = htonl( pos ); /* seek position */
171 ((int *)m.data)[1] = htonl( cnt ); /* no of packets */
172 strcpy(m.data+2*sizeof(int),file);
173 sock_cli_crc_send(sock,&m,HEAD+2*sizeof(int)+strlen(m.data+2*sizeof(int))+1);
177 int size=sock_cli_crc_recv( sock, &m, sizeof(m) );
178 if( size==0 ) break; /* timeout */
179 if( size>0 ) bytes_received += size;
181 if( packno<0 || packno>=file_packets ) continue;
182 if( file_table[packno]!='\0' ){
187 fseek(fdest,packno*DATA_PIECE,0L);
188 if( fwrite(m.data,size,1,fdest)!=1 ){
189 printf("can't write file %s\n",file);
192 graph_point(packno,GRAPH_COL_OK);
193 file_table[packno]='\1';
194 }else /* error in packet */
195 graph_point(packno,GRAPH_COL_ERROR);