Tuesday 1 October 2013

copying from buffer to buffer with a for loop working for all cases except for 0, stumped

copying from buffer to buffer with a for loop working for all cases except
for 0, stumped

I am currently trying to write a small program that tests a smaller
functionality of a udp socket program. This section of code opens a file,
stores the contents of the file into a buffer and then writes every 1024
bytes of the buffer into a section of another buffer (which is going to be
sent as packets in the udp application), I also store a sequence number at
the beginning of each packet. The file_buffer gets correctly filled but
the packet_buffer does not. (at packet_buffer[0], it is all null)
The problem is that this works for all cases except for the 0th place in
the buffer. Here everything is null.
My code is below, I have been stumped for hours trying to debug this
little error.
Any tips, tricks, solutions?
#include "sendto_.h"
#define MAXBUFSIZE 102400
#define WINDOWSIZE 6
#define THEWINDOW 100
int main (int argc, char * argv[])
{
/** file pointer to point to file opened by client to be */
FILE* fp; /**sent to the server*/
/** Identifiers for the start and the*/
int window_start, window_end; /** end of the window */
/** window for GBN sliding window */
char window[THEWINDOW];
/** buffer to hold the contents of the file to be sent to server */
char *file_buffer;
/*set of buffers to be used to send approprate window packets */
char packet_buffer[100][1024];
int sequence_id = 0;
int i, j, size, read_len;
if(!(fp = fopen(argv[1], "r")))
fprintf(stderr, "Error opening '%s': %s\n", argv[1],
strerror(errno));
// obtain file size:
fseek (fp , 0 , SEEK_END);
size = ftell (fp);
rewind (fp);
file_buffer = (char *) malloc(size * sizeof(char));
/** size */
read_len = fread(file_buffer, 1, size, fp);
if(read_len > MAXBUFSIZE){
printf("file size is too large: %d\n Exiting safely\n", read_len);
free(file_buffer);
return 1;
}
//printf("file buffer: %s", file_buffer);
//snprintf(size_buffer, 10, "%d", read_len);
/******************
sendto_() sends immediately.
it will report an error if the message fails to leave the computer
however, with UDP, there is no error if the message is lost in the
network once it leaves the computer.
******************/
//bzero(buffer,sizeof(buffer));
/*loop that is going to deal with the main functionality of this
program: sliding window, sending packets and receiving ACKS.*/
int k = 0;
int count = 0;
int temp;
/*setting up packets to be sent */
for(i = 0; i < THEWINDOW;i++){
temp = i;
packet_buffer[i][0] = i;
sequence_id = packet_buffer[i][0];
printf("sequence # of packet = %d\n", sequence_id);
for(j = 0; j < 1025; j++){
packet_buffer[i][j+1] = file_buffer[(j)+k];
count += 1;
if(count == size){
j = 1026;
i = THEWINDOW;
}
}
printf("count: %d\n", count);
printf("Wrote section of file into array: %s\n",
packet_buffer[temp]);
k += 1025;
//read info into each set of the packet buffer
}
window_start = 0;
window_end = 5;
return 0;
}

No comments:

Post a Comment