here's more if you really want to:
Code:
while (checkFullSlots() || (!feof(stdin)))
{
/*read data from stdin and fill any empty slots*/
while (checkEmptySlots() && (!feof(stdin))){
sigprocmask(SIG_BLOCK, &set , &oset);
LFS++;
Frame* frame = (Frame*) calloc(1, sizeof(Frame));
size = fread(frame->body, 1 , 256, stdin);
printf("size read to body is %d\n\n", size);
fwrite(frame->body , 1 , 256 , stdout);
/*
if (size != 256){
printf("Error: Can't read the frame body from stdin %d\n", (int) sizeof(frame->body));
close(sock_fd);
exit(1);
}
*/
frame->hdr.type = TYPE_DATA;
frame->hdr.seq = (SeqNum) LFS;
if (size == 256)
frame->hdr.size = 0;
else
frame->hdr.size = (u_char) size;
printf("seq number %d \nsize is %d \n", (int) frame->hdr.seq, (int) size);
if (feof(stdin))
frame->hdr.flags = FLAG_END;
time_t frame_timeout = time(NULL) + timeout;
if (insertTimeoutAndFrame(frame, frame_timeout, LFS)){
perror("Error: Failed inserting frame and timeout into window slot \n");
exit(1);
}
flag = sendto(sock_fd,(void*)frame ,sizeof(Frame),0,(struct sockaddr *)&sockin, sizeof(sockin));
if (flag == -1)
{
perror("Error: Sending frame failed \n");
close(sock_fd);
exit(1);
}
printf("sending frame with size %d \n", flag);
sigprocmask(SIG_SETMASK, &oset, NULL);
}
/*read ACK from socket (recv)*/
printf("Trying to receive ACK here!\n");
Frame *ACK = (Frame*) malloc(sizeof(Frame));
flag = recvfrom(sock_fd, ACK, sizeof(Frame), 0, (struct sockaddr *) 0, (socklen_t *) 0);
if (flag == -1)
{
perror("Error: Receiving ACK failed \n");
close(sock_fd);
exit(1);
}
printf("Receiving ACK for seq %d\n", ACK->hdr.seq);
sigemptyset(&set);
sigaddset(&set, SIGALRM);
sigprocmask(SIG_BLOCK, &set , &oset);
if ((int) ACK->hdr.seq > LAR){
int i;
do {
printf("emptying slot!\n");
/*empty slot containing frame LAR*/
LAR++;
for (i = 0; i < SWS; i++){
if (win_slots[i].frame->hdr.seq == LAR){
free(win_slots[i].frame);
win_slots[i].frame = NULL;
win_slots[i].timeout = -1;
}
}
} while (LAR != ACK->hdr.seq);
}
free(ACK);
printf("ready to send as there's an empty slot available!\n");
sigprocmask(SIG_SETMASK, &oset, NULL);
}