// Reliably sends an array of packets void rdt_send_packets(struct packet *packets, int sockfd, struct sockaddr_in cli_addr, socklen_t clilen, int cwndsize) { int nextseqnum = 0; int base = 0; int all_sent = 0; int i; int timer_running = 0; clock_t timer_start = 0; while(!all_sent || (base < nextseqnum)) { while (nextseqnum < base + cwndsize && !all_sent) { send_packet(packets[nextseqnum], sockfd, cli_addr, clilen); if (base == nextseqnum) { timer_running = 1; timer_start = clock(); } if (packets[nextseqnum].type == TYPE_FINAL_DATA) { all_sent = 1; nextseqnum++; break; } nextseqnum++; } int ack = listen_for_ack(sockfd); if (ack != -99) { base = ack + 1; if (base == nextseqnum) { timer_running = 0; } else { timer_running = 1; timer_start = clock(); } } // Check for timeout // fprintf(stderr, "timer_running: %d. diff: %f. RTO: %d\n", timer_running, diff_ms(timer_start, clock()), RETRANSMIT_TIMEOUT); if (timer_running && ( diff_ms(timer_start, clock()) > RETRANSMIT_TIMEOUT ) ) { printf("Timeout on packet %d. Retransmitting packet(s) %d to %d:\n", base, base, nextseqnum-1); timer_running = 1; timer_start = clock(); for (i = base; i < nextseqnum; i++) { send_packet(packets[i], sockfd, cli_addr, clilen); } } } }
void read_file(int n){ if( !feof(fp) ){ int i; for(i =0; i < n; i++ ){ bzero(buffer,191); fread(buffer,sizeof(char),191,fp); send_data(i); } listen_for_ack(); } }