Изменение порядка пакетов UDP - PullRequest
0 голосов
/ 18 марта 2019

У меня в настоящее время есть клиентская программа UDP для приема пакетов от сервера, добавляя каждый пакет в полный массив символов сообщения, пока не останется ни одного.

Теперь у меня есть задание, где я должен запустить сервер с пакетами без передачи, а затем отсортировать их по порядку на стороне клиента.

Хотя я понимаю, что номер пакетасохраненные в первых двух символах тега buf, я просто застрял в лучшем способе написать их по порядку.

Нужно ли ждать, пока все пакеты будут записаны с включенным тегом, изменить порядок, а затем удалить теги?Или есть более простой способ сделать это, который просто дует мне в голову?

Параметры и переменные:

//This char pointer is used to remove the tag from the start of each message
//Buf is set equal to this then this is set equal to the location of the char array
//where the data starts
char *buf_pointer;

//Used to hold the full poem
char fullMessage[MAXBUF];

//Used to hold the name of incoming file
char title[MAXBUF];
char *titlePtr;

//Boolean value to stop the loop when the full message is recieved
bool continue_loop = true;

//Boolean value to determine if it is the first iteration of the loop
bool first_time = true;

Цикл для получения и записи пакетов:

//While loop to repeat recvfrom until there is no data left
while(continue_loop){

    retval = select(sockfd+1, &rfds, NULL, NULL, &tv);

    //An error occured with select
    if (retval == -1) perror("select()");

    //Data is available from sockfd
    else if (retval){

        //The first data packet is formatted differently, and thus
        //will need to be handled differently
        if(first_time){
            nread = recvfrom(sockfd,title,MAXBUF,0,NULL,NULL);

            if (nread < 0) {
                perror("CLIENT: Problem in recvfrom");
                exit(1);
            }

            else {
                titlePtr = title;
                titlePtr = &titlePtr[8];  //Removing tag from the first packet
                first_time = false;
            }
        }

        else{
            nread = recvfrom(sockfd,buf,MAXBUF,0,NULL,NULL);

            if (nread < 0) {
                perror("CLIENT: Problem in recvfrom");
                exit(1);

            }

            else {
                totalBytes += nread;    //Incrementing total number of bytes
                buf_pointer = buf;    //Setting received data to char pointer
                buf_pointer = &buf_pointer[6];    //Removing tag from packet
                strcat(fullMessage, buf_pointer);   //Adding message to full char array
            }
        }
    }

    //No messages read for 1 second
    else continue_loop = false;
}
...