我目前正在尝试编写一个小程序来测试 udp 套接字程序的较小功能。这部分代码打开一个文件,将文件的内容存储到缓冲区中,然后将缓冲区的每 1024 字节写入另一个缓冲区的一部分(将在 udp 应用程序中作为数据包发送),我还存储每个数据包开头的序列号。file_buffer 被正确填充,但 packet_buffer 没有。(在 packet_buffer[0] 处,全部为空)
问题是这适用于除缓冲区中第 0 位之外的所有情况。这里一切都是空的。
我的代码在下面,试图调试这个小错误我已经被难住了好几个小时。
任何提示,技巧,解决方案?
#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;
}