SFML community forums

Help => Network => Topic started by: lordseanington on January 20, 2015, 01:56:28 am

Title: sf::Packet Data Extraction
Post by: lordseanington on January 20, 2015, 01:56:28 am
When getting data, must you do all of your extractions in one line of code or can you do one extraction. And then, when you do the next, have it continue where you last were at. Example:

packet <<x <<y;
packet >>x;
packet >>y;

If not, is there any way to do it.
Title: Re: sf::Packet Data Extraction
Post by: Ixrec on January 20, 2015, 01:57:51 am
It should "just work" either way.  These are standard C++ operators implemented in the standard way, so they behave the same as iostreams.

Is there some specific reason you're asking this question instead of simply writing that code?
Title: Re: sf::Packet Data Extraction
Post by: lordseanington on January 20, 2015, 02:10:58 am
I am looping to extract data to a map structure. It gets the x and y values from the packet, and tests to make sure they are within map range. If so, it sets the map data [ x ][ y ] to the block received. It loops this until the map reading is complete. I am getting some strange results from this method.
Title: Re: sf::Packet Data Extraction
Post by: Ixrec on January 20, 2015, 02:15:06 am
You should post a minimal and complete code example so we can pin down exactly what the problem is.

Solely based on what you posted, my blind guess would be that you're sending your map data without any size parameter, and trying to compensate for this on the receiver with a heuristic (aka, a hack) that isn't accurate enough.  If so, the solution is to send a size parameter.
Title: Re: sf::Packet Data Extraction
Post by: lordseanington on January 20, 2015, 02:46:55 am
Cilent
while(!mainpack.endOfPacket())
{
        if(mainpack >>counter1 >> counter2 >>actualx >>actualy >>temp)
        input=0;
    else
    {
        std::cout <<"An error has occured\n";
        std::cout <<"Got Map Data x " <<counter1 <<" y " <<counter2<<" data " <<temp <<" actualx " <<actualx <<" actualy "<<actualy <<"\n";
        counter1= -40; continue;
    }
    std::cout <<"Got Map Data x " <<counter1 <<" y " <<counter2<<" data " <<temp <<" actualx " <<actualx <<" actualy "<<actualy <<"\n";
    if(counter1 > 0 & counter2 > 0 & counter1 <MAP_WIDTH & counter2<MAP_HEIGHT)
    mainmap.block[counter1][counter2]=temp;
    else
    {
        if(counter1 < 1 | counter2 < 1)
    std::cout <<"An Error has Occured Because x " <<counter1 <<" or y " <<counter2 <<" is less than one\n";

        if(counter1 >MAP_WIDTH & counter2 > MAP_HEIGHT)
    std::cout <<"An Error has Occured Because x " << actualx <<" is greater than map width"
    <<MAP_WIDTH <<"\n or y " <<counter2 <<" is greater than map height " <<MAP_HEIGHT <<"\n";
    }

Server:

        upperleftx=10;//(player_.location[counter][1]-VIEW_WIDTH)-1;
        upperlefty=10;//(player_.location[counter][2]-VIEW_HEIGHT)-1;
        lowerrightx=10+VIEW_WIDTH;//(player_.location[counter][1]+VIEW_WIDTH)+1;
        lowerrighty=10+VIEW_HEIGHT;//(player_.location[counter][2]+VIEW_HEIGHT)+1;
        int counterx = upperleftx; int countery = upperlefty;
        int counter2=1; int counter1=0;
while(countery<lowerrighty)
{

    receive <<mainmap.block[counterx][countery];
    std::cout <<"mainmap screenx  " <<counter1 <<" screeny" <<counter2 <<" " << "\nx" <<counterx <<" y" << countery <<"data" <<mainmap.block[counterx][countery] <<"\n";
    counterx++;
    counter1++;
    if(counterx>lowerrightx)
    {
        counterx=upperleftx;
        counter1=1;
        countery++;
        counter2++;
    }
}
if(request.send(receive, receiveip, port) ==sf::Socket::Done)
{
    std::cout <<"Sent Properly \n";
}
else
{
    std::cout <<"An Error Occured While Sending Sending Map to " <<usernametemp <<"\n";
}
        }
 
Title: Re: sf::Packet Data Extraction
Post by: Rosme on January 20, 2015, 04:37:31 pm
This is not a Complete and Minimal Code (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).