#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
int main() {
sf::UdpSocket socket;
sf::IpAddress ip = "84.195.14.184";
unsigned short port = 54000;
size_t x = 100;
sf::Socket::Status status;
char data[50] = "Test";
//Send part
status = socket.send(data, x, ip, port);
if (status == sf::Socket::Done) {
cout << "[Announcement] Packed sended successfull." <<endl;
} else {
cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
}
//Receive part
char receivedPacket[50];
status = socket.receive(receivedPacket, sizeof(receivedPacket) +1, x, ip, port);
if (status == sf::Socket::Done) {
cout << "[Announcement] Packet received succesfull." <<endl;
} else {
cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
}
cin.ignore().get();
}
Tried again from scratch and failed again.
However, I think I saw something strange...
If you look at the receive function on line 27.
When looking at the documentation, the parameters are:
(data, size, received, remoteAddress, remotePort).
Size is the maximum numbers I can receive,
received is the actual number of bytes I received.
So you'd expect, that these should be my parameters:
(receivedPacket, x, sizeof(receivedPacket), ip, port)x: I manually set a maximum I want to receiver,
sizeof(receivedPacket): Parameter gets (actual) size of packet.
Weird enough, if I take this order I get an error.
Why is that?
I personally don't understand that part.