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

Author Topic: Udp sockets don't seem to be working  (Read 2706 times)

0 Members and 1 Guest are viewing this topic.

3N1GM4

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Udp sockets don't seem to be working
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Udp sockets don't seem to be working
« Reply #1 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?
Laurent Gomila - SFML developer

3N1GM4

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Udp sockets don't seem to be working
« Reply #2 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.