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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - DinoMijatovic

Pages: [1]
1
Network / Re: Values in UDP packets are corrupted
« on: January 10, 2014, 12:10:07 am »
Quote
ReceivePlayerData() should take the parameter by reference. Otherwise, the packet remains local to the function.

Thank you so much, this was exactly it. ;D

Mistakes are nothing more than learning experiences, huh?

2
Network / Re: Values in UDP packets are corrupted
« on: January 09, 2014, 10:59:58 pm »
Quote
What if you send the packets to the localhost?

I've tried that there, I'm still getting the same result.

Quote
Try to minimize the code until the you can blame a specific, small part of it for the problem.

I'm pretty sure that the client receive stuff is where it all goes wrong, as after quite a bit of couts, thats the only time where the values in the packet aren't what were sent.

3
Network / Re: Values in UDP packets are corrupted
« on: January 09, 2014, 09:47:14 pm »
Hey, thanks!

Sorry, it's my first time posting, so I may have posted a bit too much stuff. I also definitely structured my question wrong :P

I appreciate you recommending TCP, but I don't want to do TCP just because I gave up on doing UDP. I don't really get anything from that, I really want to try and finish this so that hopefully when I get it working I understand why it works, and can duplicate on a larger project later on. If you're telling me to give up on UDP, at least tell me what I'm doing wrong so that I can walk away from it having learned something new!

4
Network / Re: Values in UDP packets are corrupted
« on: January 09, 2014, 08:35:02 pm »
Anyone got any clue? Not sure how long it usually takes for someone to reply to these things.

5
Network / Values in UDP packets are corrupted
« on: January 09, 2014, 06:55:36 pm »
Hey guys, I am making a simple game using SFML. Currently I've just got 2 rectangle shape players that move about. I'm trying to send UDP packets that contain 2 floats that store a client's X and Y positions to a server, which then forwards that very same packet to the other client. I've done a fair bit of debugging, and as far as I can tell, the values in the packet are correct up until the point that they are actually received by a client where, when extracted, the values are 0 and 0. I'm not really sure how to go about fixing this, as I am fairly new to SFML and network programming. I'll post the code that I think is relevant below

server main
//Used to send Player positions between clients
struct playerData
        {
                sf::Vector2f Position;
        };
       
        //Used to store ClientIDs, as well ass their addresses and the ports the client sockets are bound to
        struct clientData
        {
                sf::IpAddress address;
                unsigned short port;
                sf::Uint16 ID;
        };

int main()
{
        clientData clients[2];

        sf::UdpSocket socket;

        sf::Uint16 SocketID = 0;
       
        sf::Packet packet;

        sf::Packet receivedPacket;

        packet << SocketID;

        bool keepRunning = true;

        sf::IpAddress sender;
       
        unsigned short port;

        unsigned short ClientPort;

        //bind the listener to a port;
        if(socket.bind(4444) == sf::Socket::Done)
        {
                //Wait for 2 clients to connect
                for(int i  = 0; i < 2; i++)
                {
                       
                        //When a client connects, it should send a packet containing the port it is bound to
                        //Server stores this in the ClientData struct, as well as the address it received from
                        //and the Client's ID
                        if(socket.receive(packet, sender, port) == sf::Socket::Done)
                        {      
                                packet >> ClientPort;

                                clients[i].address = sender;
                                clients[i].port = ClientPort;
                                clients[i].ID = SocketID;

                                SocketID += 1;

                                packet.clear();


                        }
                }

               
                //Send back the Client's ID
                for(int i = 0; i < 2; i++)
                {
                        packet << clients[i].ID;
                        socket.send(packet, clients[i].address, clients[i].port);
                        packet.clear();

                }

               
                while(keepRunning == true)
                {
                       
                       
                        //Receive a packet containing a client's position in the game world
                        //Then send that same packet to the other client
                       
                        if(socket.receive(packet, clients[1].address, clients[1].port) == sf::Socket::Done)
                        {
                       
                                socket.send(packet, clients[0].address, clients[0].port);
                                cout<<"Sent info to player 2"<<endl;

                                packet.clear();

                        }
                       
                       
                        if(socket.receive(packet, clients[0].address, clients[0].port) == sf::Socket::Done)
                        {

                                socket.send(packet, clients[1].address, clients[1].port);
                                cout<<"Sent info to player 1"<<endl;

                                packet.clear();

                        }
                }


Client class functions:
//Binds the client to a free port, and then stores that port to be sent to the server to be stored
void client::Bind()
{
        bind(myPort);

        myPort = getLocalPort();
}


//sends the client port to server, and in return gets its ID
Uint16 client::getID()
{
        packet << myPort;

        std::cout<<myPort<<std::endl;

        send(packet, serverAddress, port);

        while(gotID == false)
        {

                std::cout<<"waiting..."<<std::endl;
                if(receive(receivedPacket, serverAddress, port) == sf::Socket::Done)
                {
                        receivedPacket>>receivedID;
                        gotID = true;
                }
        }

        setBlocking(false);

        return receivedID;
}

void client::SendPlayerData(Packet packet)
{
        send(packet, serverAddress, port);
}

bool client::ReceivePlayerData(Packet packet)
{      
        if(receive(packet, serverAddress, port) == sf::Socket::Done)
                return true;

}


Client main:
sf::Uint32 OtherX;
sf::Uint32 OtherY;

//used tp send player's position to server
sf::Uint32 Xposition;
sf::Uint32 Yposition;

int main()
{

        client Client;

        player Players[2];

        Players[0].setFillColor(sf::Color::Red);
        Players[1].setFillColor(sf::Color::Blue);

        Players[0].setPosition(200.f, 400.f);
        Players[1].setPosition(300.f, 300.f);

sf::Packet packet;

        sf::Packet Receivedpacket;

       
        Client.Bind();

        Client.getID();
       

        cout<<"gotID"<< Client.receivedID<<endl;

        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

       

                // run the program as long as the window is open
                while (window.isOpen())
                {
                        // check all the window's events that were triggered since the last iteration of the loop
                        sf::Event event;
                        while (window.pollEvent(event))
                        {
                                // "close requested" event: we close the window
                                if (event.type == sf::Event::Closed)
                                        window.close();
                        }

                        //make sure only this client's player can be controlled by this keyboard
                        Players[Client.receivedID].Movement(Client.receivedID);

                        cout<<"My ID: "<< Client.receivedID<<endl;


                        //Ready this player's data to be sent to the server
                        Xposition = Players[Client.receivedID].getPosition().x;
                        Yposition = Players[Client.receivedID].getPosition().y;

                        packet << Xposition <<Yposition;

                        Client.SendPlayerData(packet);

                        packet.clear();
                       
                       
                        //Receive information about other player
                        if(Client.ReceivePlayerData(Receivedpacket))
                        {
                                Receivedpacket >> OtherX >> OtherY;

                        }

if(Client.receivedID == 1)
                                        Players[0].setPosition(OtherX, OtherY);
                               
                                else if(Client.receivedID == 0)
                                        Players[1].setPosition(OtherX, OtherY);


                                Receivedpacket.clear();
                        }

// clear the window with black color
                        window.clear(sf::Color::Black);
                         
                        // draw everything here...
                        for(int i = 0; i < 2; i++)
                                window.draw(Players[i]);
                       

                        // end the current frame
                        window.display();
                }
       

    return 0;
}

 


Like I said, I'm pretty to new to networking and SFML, so any help at all here is really appreciated :)

Thank you!

P.S. I apologise for the wall of code  :-[

Pages: [1]
anything