Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Receiving random data  (Read 2442 times)

0 Members and 1 Guest are viewing this topic.

rotenKleber

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Receiving random data
« on: October 10, 2017, 09:47:41 pm »
While trying to send map tiles from a server to a client, I noticed that there were other values interspersed throughout the packet that really should not be there.
The data at the top is the map that should be sent. The data near the bottom is the data that was received. I have highlighted some of the anomalies



To clarify a bit, I am trying to load the entire map onto a single packet, with this bit here
(click to show/hide)

When pulling out the code, I just do the opposite, extract the "code" telling the client what to do with the info, grab the size of the map, then load all the tiles sent into the map. The issue occurs when there are numbers in the received data that shouldn't be in there, as seen in the image.

Has anyone else experienced this? Does anyone know what this data is / how it gets into the packet or received / how to remove it or prevent it?
« Last Edit: October 10, 2017, 09:52:05 pm by rotenKleber »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Receiving random data
« Reply #1 on: October 10, 2017, 10:47:41 pm »
You should print the raw content of the packet right before sending it, and right after receiving it. That would help to figure out if the data gets corrupted before sending, during transmission, or after receiving.
Laurent Gomila - SFML developer

rotenKleber

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Receiving random data
« Reply #2 on: October 10, 2017, 11:06:50 pm »
Thanks for the reply! I appreciate your work, by the way.

How would I print the raw data of the packet without disassembling it?

I tried using Packet::get_data(), but I'm not sure what to cast the void pointer to. I tried this with strange results:

std::printf("Data before sending: %d\n", *(reinterpret_cast<const long long*>(data.getData())));

The output being
Code: [Select]
73728
But that const long long* was a wild guess and it changes drastically depending on what I cast it to, not sure if that's the best indicator. Either way, I figured out that the error is occurring when attempting to pull the tiles out of the packet with

for (unsigned int x = 0; x < size; x++) {
    for (unsigned int y = 0; y < size; y++) {
         sf::Uint8 type;

         data >> type;

         printf("Type: %d\n", type);

         map[x][y] = (tile) ((int)type);
     }
}
 
« Last Edit: October 10, 2017, 11:41:35 pm by rotenKleber »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Receiving random data
« Reply #3 on: October 11, 2017, 02:35:49 pm »
Cast the pointer to unsigned char pointer and iterate through it and print the bytes as hex numbers. Something like:
void printdata(const void * data, int len)
{
        const unsigned char * ptr = reinterpret_cast<const unsigned char*>(data);
        for(int i = 0; i < len; ++i)
                std::printf("%02x ", ptr[i]);
 
        std::printf("\n");
}
Back to C++ gamedev with SFML in May 2023