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

Author Topic: UDP Socket does not connect  (Read 2947 times)

0 Members and 1 Guest are viewing this topic.

mickes27

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
UDP Socket does not connect
« on: March 10, 2018, 12:03:10 am »
Hey, I want to send some packet which contains: command, pin number and value from my PC to Raspberry Pi via UDP. I made this code:

PC:
#include <SFML/Network.hpp>
#include <iostream>

int main() {

        sf::UdpSocket socket;
        socket.setBlocking(false);

        sf::Packet packet;
        sf::String command = "write";
        sf::Uint16 pin = 23;
        sf::Uint16 value = 0;

        packet << command << pin << value;

        sf::IpAddress recipient = "192.168.1.102";
        unsigned short port = 54000;

        while (1) {
                if (socket.send(packet, recipient, port) != sf::Socket::Done)
                {
                        std::cout << "Couldn't send" << std::endl;
                }
                else {
                        std::cout << value << std::endl;
                        value++;
                }
        }


        return 0;
}

Rpi:
#include <SFML/Network.hpp>
#include <iostream>

int main() {

        sf::UdpSocket socket;
        socket.setBlocking(false);

        if (socket.bind(54000) != sf::Socket::Done)
        {
                std::cout << "Cannot bind socket" << std::endl;
        }

        sf::Packet packet;
        sf::IpAddress remoteIP = "192.168.1.101";
        unsigned short remotePort = 54000;
       
        while (1) {
                if (socket.receive(packet, remoteIP, remotePort) == sf::Socket::Done) {
                        std::cout << "Got it" << std::endl;
                        sf::String command;
                        sf::Uint16 pin;
                        sf::Uint16 value;

                        packet >> command >> pin >> value;
                        std::cout << "Command: " << command.toAnsiString().c_str() << " " << pin << " " << value << std::endl;
                }
                else if (socket.receive(packet, remoteIP, remotePort) == sf::Socket::NotReady) {
                        std::cout << "Some error while recieving" << std::endl;
                }
        }


        return 0;
}
 

But I have some error while running both programs, cause it says packet is sent from PC, but it's still "Some error while recieving" on Rpi. What thing I failed and missunderstand?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: UDP Socket does not connect
« Reply #1 on: March 10, 2018, 08:48:58 am »
You're calling the function (ie. trying to receive data) twice, just for testing the return value. Which doesn't make sense and is of course wrong.

Do this instead:
auto status = socket.receive(packet, remoteIP, remotePort);
if (status == sf::Socket::Done)
{
    // ...
}
else if (status == sf::Socket::NotReady)
{
    // ...
}

And by the way, "NotReady" is not an error, it just means that there's nothing to be received yet on your non-blocking socket.
Laurent Gomila - SFML developer

mickes27

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: UDP Socket does not connect
« Reply #2 on: March 10, 2018, 11:40:17 am »
Oh, you are right, my bad. But even in this way, application never get packet. It is send from PC, but on Rpi there is always NotReady, and never Done. Something like it lost package meanwhile.

EDIT: I think I got it:

PC:
#include <SFML/Network.hpp>
#include <iostream>

int main() {

        sf::UdpSocket socket;
        socket.setBlocking(false);

        sf::Packet packet;
        sf::String command = "write";
        sf::Uint16 pin = 23;
        sf::Uint16 value = 0;

        packet << command << pin << value;

        sf::IpAddress recipient = "192.168.1.102";
        unsigned short port = 54000;

        while (1) {
                packet.clear();
                packet << command << pin << value;
                if (socket.send(packet, recipient, port) != sf::Socket::Done)
                {
                        std::cout << "Some error" << std::endl;
                }
                else {
                        std::cout << value << std::endl;
                        if (value < 10) {
                                value++;
                        }
                        else {
                                value = 0;
                        }
                       
                }
        }

                system("PAUSE");

        return 0;
}

Rpi:
#include <SFML/Network.hpp>
#include <SFML/Network.hpp>
#include <iostream>

int main() {

        sf::UdpSocket socket;
        socket.setBlocking(false);

        if (socket.bind(54000) != sf::Socket::Done)
        {
                std::cout << "Cannot bind socket" << std::endl;
        }

        sf::Packet packet;
        sf::IpAddress remoteIP;// = "192.168.1.100";
        unsigned short remotePort;// = 54000;
       
        while (1) {
                auto status = socket.receive(packet, remoteIP, remotePort);
                if (status == sf::Socket::Done){
                        std::cout << "Got it" << std::endl;
                        sf::String command;
                        sf::Uint16 pin;
                        sf::Uint16 value;

                        packet >> command >> pin >> value;
                        std::cout << remoteIP  << ": "<< "Command: " << command.toAnsiString().c_str() << " " << pin << " " << value << std::endl;
                }
                else if (status == sf::Socket::NotReady){
                        std::cout << "No data" << std::endl;
                }
        }


        return 0;
}

 

Does remoteIP and remotePort has to be empty?
« Last Edit: March 10, 2018, 11:53:21 am by mickes27 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: UDP Socket does not connect
« Reply #3 on: March 10, 2018, 05:44:50 pm »
Quote
Does remoteIP and remotePort has to be empty?
It doesn't matter, they are out arguments, which means that they are overwritten by the function if something is received.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10976
    • View Profile
    • development blog
    • Email
Re: UDP Socket does not connect
« Reply #4 on: March 11, 2018, 12:16:27 pm »
Start up Wireshark and similar and check your network traffic.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/