SFML community forums

Help => Network => Topic started by: 3N1GM4 on September 11, 2013, 06:41:40 pm

Title: Udp sockets don't seem to be working
Post by: 3N1GM4 on September 11, 2013, 06:41:40 pm
So I tried creating an application using udp sockets. I messed around with it a bit and didn't seem to be able to get it to work.. so I decided to just copy/paste the example code from the class reference.

void main()
{
char ctype;
cin>>ctype;
if (ctype=='c')
{
        // ----- The client -----
        // Create a socket and bind it to the port 55001
        sf::UdpSocket socket;
        socket.bind(55001);
        // Send a message to 192.168.1.50 on port 55002
        std::string message = "Hi, I am " + sf::IpAddress::getLocalAddress().toString();
        socket.send(message.c_str(), message.size() + 1, "192.168.1.50", 55002);
        // Receive an answer (most likely from 192.168.1.50, but could be anyone else)
        char buffer[1024];
        std::size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;
        socket.receive(buffer, sizeof(buffer), received, sender, port);
        std::cout << sender.toString() << " said: " << buffer << std::endl;
}
else
{
        // ----- The server -----
        // Create a socket and bind it to the port 55002
        sf::UdpSocket socket;
        socket.bind(55002);
        // Receive a message from anyone
        char buffer[1024];
        std::size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;
        socket.receive(buffer, sizeof(buffer), received, sender, port);
        std::cout << sender.toString() << " said: " << buffer << std::endl;
        // Send an answer
        std::string message = "Welcome " + sender.toString();
        socket.send(message.c_str(), message.size() + 1, sender, port);
}
}

Compiles without errors but when I run the application, nothing happens.
I should probably say that the two computers I'm using are connected to the internet using a wireless router and have the same Public IP.

A solution to this beginners' problem would be appreciated a lot- maybe I'm just missing something really simple..?

Thanks in advance
Title: Re: Udp sockets don't seem to be working
Post by: Laurent on September 11, 2013, 07:23:20 pm
The SFML SDK has a simple example of UDP socket communication which works. Have you tried it?
Title: Re: Udp sockets don't seem to be working
Post by: 3N1GM4 on September 12, 2013, 07:52:38 am
Forgot to add an exception to my firewall.. so yes, it works now-somewhat.
Sockets send and receive perfectly when the two computers share a public ip, but when their public ip's are different nothing happens.