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

Author Topic: UDP Network pong game  (Read 2201 times)

0 Members and 1 Guest are viewing this topic.

kozarecvode

  • Newbie
  • *
  • Posts: 1
    • View Profile
UDP Network pong game
« on: June 03, 2013, 04:32:04 pm »
I'm trying to create a simple network pong game and since I'm totally new to network programming I stumbled upon a problem here..
I have a client and a server and client CAN send packets to the server and the server CAN receive that packet. The problem however is when I try to send a packet from server to a client. The packet is sent but the client can't recieve it. I'm testing this on my own computer.
Some code..
SERVER:
if(a){ // if I am server
   sf::Packet s,g,s2,g2;
        if(socket.receive(g,globalni,port3) == sf::Socket::Done){
                                           g>>poz;
                                        cout<<poz << endl;
                                        rightPaddle.setPosition(gameWidth - 10 - paddleSize.x / 2,poz);  }
                                else cout <<"Paket ni sprejet ali pa ni bil poslan :(" << endl;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
                   (leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
                   {   leftPaddle.move(0.f, -paddleSpeed * deltaTime);
                        x2=leftPaddle.getPosition().y;
                        s2 << x2;
if(socket.send(s2,"192.168.1.5",port3)==(sf::Socket::Done)){ cout <<x2 << endl;    } else cout <<"Paket ni poslan" << endl;
                                     }

     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
                   (leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
                   {   leftPaddle.move(0.f, -paddleSpeed * deltaTime);
                                x2=leftPaddle.getPosition().y;
                                s2 << x2;
                                if(socket.send(s2,"192.168.1.5",port3)==(sf::Socket::Done)){ cout <<x2 << endl;    } else cout <<"Paket ni poslan" << endl;
}
 
CLIENT:
if(!a){
sf::Packet s,g,s2,g2;
socket2.setBlocking(0);
if(socket2.receive(g2,novi,porta) == sf::Socket::Done){  //novi="192.168.1.5"
                                           g2>>poz2;
                                        cout<<poz2 << endl;
                        leftPaddle.setPosition(10 + paddleSize.x / 2,poz2);  }


        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) &&
               (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
               {
                rightPaddle.move(0.f, -paddleSpeed * deltaTime);
                                x=rightPaddle.getPosition().y;
                                s << x;
                                if(socket2.send(s,"192.168.1.5",porta)==(sf::Socket::Done)){ cout <<x << endl;tri=globalni;porthaha=port; }else cout <<"Paket ni poslan" << endl;
            }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) &&
               (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)){
                           rightPaddle.move(0.f, paddleSpeed * deltaTime);

               
                         
                                x=rightPaddle.getPosition().y;
                                s << x;
                       
                                if(socket2.send(s,"192.168.1.5",porta)==(sf::Socket::Done)) cout <<x << endl; else cout <<"Paket ni poslan" << endl;

                        }
}
}
 
As you can see I'm using two different sockets.
Please help me; I'm quite new at programming and if you need additional code or information I will happily provide it. Thank you!

I forgot to mention that if the client is sending packets, the client can also recieve the packet from the server, otherwise it can't.
« Last Edit: June 03, 2013, 09:32:53 pm by kozarecvode »