0

I have a struct which has a char array for text. This struct will be sent over UDP. On the sending side there is no problem in displaying the text. The text will in most cases be of length equal to the size of the char array. When using cout on the sending side, the text is shown with no errors. However, on the receiving side, there appears a garbage value every time.

On the sending side:

bzero(c, DATA_SIZE+1);
file.read(c, DATA_SIZE);
strcpy(window[i].data, c);
cout << window[i].data; // this shows the text correctly

On the receiving side:

bzero(&recd_packet, sizeof(rudp_packet));
recvfrom(udp_socket, &recd_packet, sizeof(rudp_packet),0,NULL, NULL);

int rec_no = recd_packet.seq_no;
cout << "\n\n\n\nSequence number: " << rec_no << "\n";
cout << recd_packet.data; // this shows garbage at the end

I'm clearing all the buffers/struct before I fill in with anything. Where am I going wrong?

EDIT: I resized the temp buffer 'c' to DATA_SIZE (1464), and copied data of length one less than DATA_SIZE. At the last position I put '\0' and it works correctly now. My question still remains, what made it work correctly on the sending side?

EDIT2: Here's the struct:

struct rudp_packet {
    bool ack;
    bool fin;
    int seq_no;
    char data[DATA_SIZE];
};
Amolbh
  • 1
  • 2

0 Answers0