1

我目前正在尝试编写一个小程序来测试 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;

}
4

3 回答 3

1

我会认为:

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
}

也许值得尝试一下。我认为您的循环正在被溢出,这不是您的主要问题,但对我来说仍然是一个令人困惑的地方。

编辑每次我看它时,我都会看到要脱离循环的东西。

于 2013-10-02T00:41:57.643 回答
0

想通了,因为我们在第一个数据包的开头将 0 存储为一个字符,它存储了一个 '\00' 字符,这使得我们用来写入文件的函数一开始就停止。感谢所有的帮助。

于 2013-10-02T19:25:14.423 回答
0

我看到一些问题。

  1. 您需要将 packet_buffer 声明为 packet_buffer[100][1025]。ID 为 1 个字节,缓冲区数据为 1024 个字节。总计 = 1025。

  2. 您的 for (j...) 循环从 0 循环到 1024。它应该从 0 循环到 1023,以便在 i 循环的每次迭代中传输总共 1024 个字节。

  3. 需要在j循环结束时给k加上1024,而不是1025。同样可以设置j=1024在count==size时退出j循环。

  4. 您想在 printf 中使用 &packet_buffer[temp][1] 因为第一个字符是索引而不是来自文件缓冲区。

于 2013-10-02T00:46:25.727 回答