c++ - Unable to send pdf through binary with UDP -
i having trouble sending pdf file client server reading file in binary. using udp transfer , when send text file client server receive file without errors when send pdf error opening file. heres have both sender , receiver:
sender:
file = fopen(file_name_char, "rb"); //read in binary here int size_count = (file_size / (buffer_size - 2)) + 1; //amount of times loop (int = 0; < size_count; i++) { memset(szbuffer, 0, buffer_size); fread(szbuffer, sizeof(char), buffer_size - 2, file); //read buffersize -2 (reserve spot bit , \0) strcpy(szbuffer, concat(current_bit, szbuffer)); send_with_select(s, szbuffer, (struct sockaddr*)&sa1, (struct sockaddr*)&sa1, sa1_length, current_bit, "file contents", 0); //send file contents file_size = file_size - buffer_size + 2; //decrease size client_bit = change_bit(client_bit); *current_bit = bit_string(client_bit); //change bit } fclose(file);
for udp transfer prepending each buffer sequence number simulate stop n wait protocol
receiver:
int size_count = (file_size / (buffer_size - 2)) + 1; //amount of times loop (int = 0; < size_count; i++) { file = fopen(file_name, "ab"); //open file writing memset(szbuffer, 0, buffer_size); receive_packet_transfer(s, szbuffer, (struct sockaddr*)&sa_in, (struct sockaddr*)&sa_in, sa_length, current_bit, content, "file contents", client_bit); //receive file contents client_bit = change_bit(client_bit); *current_bit = bit_string(client_bit); //change bit if (file_size <= buffer_size) fwrite(content, sizeof(char), file_size, file); else fwrite(content, sizeof(char), buffer_size - 2, file); //write file file_size = file_size - buffer_size + 2; //decrease size fclose(file); }
i have implemented send_with_select() , receive_packet_transfer() using sendto() recvfrom() methods. same thing wait ack or timeout (using select).
strcpy(szbuffer, concat(current_bit, szbuffer));
string manipulation functions strcpy
, strlen
etc work textual data cannot contain \0
because consider \0
end of string. pdf binary , can contain \0
. strcpy
copy data first \0
.
apart that, using udp transfer files bad idea unless make sure can deal lost packets, duplicate packets , packet reordering. better use tcp cares this.
Comments
Post a Comment