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