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

Author Topic: UDP socket.send and socket.receive collide  (Read 2573 times)

0 Members and 1 Guest are viewing this topic.

Veeper

  • Newbie
  • *
  • Posts: 9
    • View Profile
UDP socket.send and socket.receive collide
« on: August 13, 2014, 06:00:34 pm »
Hello guys! I have a problem with a client: if I only use both socket.send and socket.receive like here:

socket.setBlocking(false);

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

    while(window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            packet << "action" << me.name << "right";
            status=socket.send(packet, serverIP, serverPort);
            packet.clear();
        }
       
        socket.receive(packet, serverIP, serverPort);
        if(packet.getDataSize()>0)
        {
            packet >> type_receive;
            if(type_receive=="movement_x")
            {
                packet >> name >> x;
                packet.clear();
                std::cout << name << "\t" << x << std::endl;
            }
        }
           
        window.clear(sf::Color::Black);
        window.display();
    }

The programme doesn't send nor receive. If I delete the socket.receive part like here:

socket.setBlocking(false);

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

    while(window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            packet << "action" << me.name << "right";
            status=socket.send(packet, serverIP, serverPort);
            packet.clear();
        }
           
        window.clear(sf::Color::Black);
        window.display();
    }

The programme works. Do you know why is that? I tried almost everything  :'(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP socket.send and socket.receive collide
« Reply #1 on: August 13, 2014, 06:59:24 pm »
Check the return value of your socket functions, and react accordingly. Especially in non-blocking mode. Make sure you've read the doc, tutorials and examples carefully.
Laurent Gomila - SFML developer

Veeper

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: UDP socket.send and socket.receive collide
« Reply #2 on: August 13, 2014, 11:44:17 pm »
EDIT: Problem solved. Thank you very much for your effort  ;D
« Last Edit: August 14, 2014, 12:11:30 am by Veeper »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: UDP socket.send and socket.receive collide
« Reply #3 on: August 14, 2014, 12:21:09 am »
EDIT: Problem solved.
For the benefit of future readers of the thread it would be nice if you described exactly what solved the problem :)

Veeper

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: UDP socket.send and socket.receive collide
« Reply #4 on: August 14, 2014, 09:47:08 am »
The problem was in:
socket.receive(packet, serverIP, serverPort);

I was overwriting serverIP and serverPort everytime I used this function. I know I shouldn't do this, but to be honest I don't know why is it such a problem(serverIP and serverPort stay the same). I guess it may have something to do with ports as I'm launching client and server from the same machine. Anyway: changing serverIP and serverPort to senderIP and senderPort solved the problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP socket.send and socket.receive collide
« Reply #5 on: August 14, 2014, 11:07:31 am »
The ports the server sends to, and receives on, are not the same (otherwise that would mean that, on the same machine, the same port is used for receiving by both the client and the server, which is impossible). That's why you have to keep them distinct.
Laurent Gomila - SFML developer