gpt4 book ai didi

使用 for 循环从缓冲区复制到缓冲区,适用于除 0 之外的所有情况,难倒

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:17 25 4
gpt4 key购买 nike

我目前正在尝试编写一个小程序来测试 udp 套接字程序的较小功能。这部分代码打开一个文件,将文件的内容存储到缓冲区中,然后将缓冲区的每 1024 字节写入另一个缓冲区的一部分(将在 udp 应用程序中作为数据包发送),我还存储每个数据包开头的序列号。 file_buffer 被正确填充但 packet_buffer 没有。 (在packet_buffer[0]处全为null)

问题是这适用于所有情况,除了缓冲区中的第 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;

}

最佳答案

我认为:

char packet_buffer[100][1024 + 1];
...
for(i = 0, count=0; i < THEWINDOW && count < size;i++){
packet_buffer[i][0] = i;
sequence_id = packet_buffer[i][0];
printf("sequence # of packet = %d\n", sequence_id);
for(j = 1; j < 1025 && count < size; ){
packet_buffer[i][j++] = file_buffer[count++];
}
printf("count: %d\n", count);
printf("Wrote section of file into array: %s\n", packet_buffer[i]);
//read info into each set of the packet buffer
}

可能值得一试。我认为你的循环正在超限,这不是你的主要问题,但仍然让我感到困惑。

编辑 每次我看它时,我都会看到要退出循环的内容。

关于使用 for 循环从缓冲区复制到缓冲区,适用于除 0 之外的所有情况,难倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com