0

I currently have a UDP Client program to receive packets from a server, adding each packet to a full message char array until none remain.

I have an assignment now where I have to start the server with out-of-transmission packets and then sort them back to order on the client side.

While I understand the packet number is stored in the first two characters of the buf tag, I'm just stuck on the best way to go about writing these in order.

Would I just have to wait until all packets are written with the tag included, reorder, and then remove tags? Or is there an easier way to go about this that is just blowing over my head?

Parameters and Variables:

//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;

Loop to receive and write packets:

//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;
}
dfg724
  • 1
  • 1
  • 1
    Use an array of pointers. The packet number can be used as the index into the array, and the value is a pointer to the packet. After you receive all the packets you can loop through the array and concatenate all the buffers. – Barmar Mar 18 '19 at 01:42
  • It mostly depends on your assignment requirements. The simpler approach is what @Barmar suggested: an array of pointers to each datagram payload would fit well if there is no particular requirement about the sorting method. Take care that you would later need the tag elements to perform the sorting so you should keep them and eventually remove them during the final concatenation. Also, what should you do with those datagrams at the end? Print them, store them or send them back? – matteo martelli Mar 18 '19 at 08:34

0 Answers0