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

Author Topic: Peer not receiving packets  (Read 5409 times)

0 Members and 1 Guest are viewing this topic.

Ago19

  • Newbie
  • *
  • Posts: 15
    • View Profile
Peer not receiving packets
« on: September 23, 2020, 06:15:26 pm »
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]
« Last Edit: October 14, 2020, 09:55:12 am by Ago19 »

 

anything