I've created a program based on UDP connection to chat with your friends just by knowing their Ip Connection. I'm testing on my local network, so the Ips are like 192.168.1.x, The program correctly sends the data but it is not received on the other end. Sorry for my bad English
#include <SFML/Network.hpp>
#include <thread>
#include <iostream>
#include <string>
unsigned short port = 2000;
sf::UdpSocket socket;
sf::IpAddress otherAddress;
sf::Packet r_packet;
sf::Packet s_packet;
void recive_data() {
sf::IpAddress tempAddress = otherAddress;
std::string data;
while (true) {
if (socket.receive(r_packet, tempAddress, port) != sf::Socket::Done) {
}
else {
std::cout << "Packet received" << std::endl;
//Print received data
r_packet >> data;
std::cout << "- " << data << std::endl;
}
}
}
int main()
{
//Set non-blocking sockets
socket.setBlocking(false);
//Binding to port to receive data
if (socket.bind(port) != sf::Socket::Done) {
std::cout << "Error binding socket to port\n";
}
//Choose sender and receiver
std::cout << "Insert the IP of who you want to talk with\n";
std::cin >> otherAddress;
//Receiving data thread
std::thread r_data(recive_data);
//Sending messages
std::string message;
while (true) {
std::cin >> message;
s_packet << message;
if (socket.send(s_packet, otherAddress, port) != sf::Socket::Done) {
std::cout << "There was a problem sending the message\n";
}
}
system("pause");
return 0;
}
[/quote]