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?