SFML community forums

Help => Network => Topic started by: Nexy on September 20, 2010, 09:27:25 am

Title: Problem with IP
Post by: Nexy on September 20, 2010, 09:27:25 am
Hello,
I've problem with server and client connection.

Server:

Quote
#include <SFML/Network.hpp>
#include <iostream>


int main()
{
    const unsigned short Port = 2435;

    sf::SocketUDP Server;
    if (!Server.Bind(Port))
    {
        std::cout << " error" << std::endl;
    }

    sf::Packet Packet;
    sf::IPAddress ClientAddress;
    unsigned short ClientPort;
    Server.Receive(Packet, ClientAddress, ClientPort);

    int a;
    Packet >> a;
    std::cout << "packet : " << a << " from "<< ClientAddress <<" port: "<< ClientPort << std::endl;

    a=12345;
    Packet.Clear();
    Packet << a;
    Server.Send(Packet, ClientAddress, ClientPort);

    std::cin.ignore(10000, '\n');
    Server.Close();
    return EXIT_SUCCESS;
}


Client:

Quote
#include <SFML/Network.hpp>
#include <iostream>


int main()
{
    const unsigned short Port = 2400;

    sf::SocketUDP Client;
    if (!Client.Bind(Port))
    {
        std::cout << " error" << std::endl;
    }

    sf::Packet Packet;
    sf::IPAddress ServerAddress;
    unsigned short ServerPort=2435;

    int a;
    a=54321;
    Packet << a;
    Client.Send(Packet, ServerAddress, ServerPort);

    Packet.Clear();
    Client.Receive(Packet, ServerAddress, ServerPort);

    Packet >> a;
    std::cout << "packet : " << a << " from "<< ServerAddress <<" port: "<< ServerPort << std::endl;

    std::cin.ignore(10000, '\n');
    Client.Close();
    return EXIT_SUCCESS;
}


When I use "IPAddress ClientAddress;" it works. But when I use:

Quote
IPAddress::GetPublicAddress();
or
Quote
IPAddress Address("83.145.154.170");
it doesn't work!

This IP is from http://whatismyip.org
Title: Problem with IP
Post by: Mindiell on September 20, 2010, 09:30:36 am
What is your network configuration ?
Have you 2 computers ?
Are you connected to the Internet ? How ?
Title: Problem with IP
Post by: Nexy on September 20, 2010, 03:27:01 pm
Thanks for reply. I'm using one computer with Motorola modem:

(http://4.bp.blogspot.com/_29mmOeNQRzc/TGwhtlEOEQI/AAAAAAAAAMg/EyWgN43n7CE/s1600/MotorolaSURFboardSB5120cablemodem-large.jpeg)

When I use
Quote
IPAddress ClientAddress;
program use "255.255.255.255", not "83.145.154.170". Why? Someone can help me?
Title: Problem with IP
Post by: Laurent on September 20, 2010, 04:16:12 pm
Quote
When I use
Quote
IPAddress ClientAddress;

program use "255.255.255.255", not "83.145.154.170". Why? Someone can help me?

It gives an invalid address because you don't put a valid address in it ;)
Code: [Select]
IPAddress ClientAddress = "83.145.154.170";
Title: Problem with IP
Post by: Nexy on September 20, 2010, 04:28:17 pm
When I use
Code: [Select]
IPAddress ClientAddress/ServerAddress = "83.145.154.170"; it doesn't work! It works only with:
Code: [Select]
IPAddress ClientAddress/ServerAddress;

Why?
Title: Problem with IP
Post by: Laurent on September 20, 2010, 04:44:03 pm
Using a default-constructed IPAddress gives an invalid address, but this address is also the UDP broadcast address. That's why you can reach everyone on your local network when using such an address, but not outside (broadcast messages are blocked by the router).

If you want to use a public IP address, make sure that the router doesn't block the corresponding port.
Title: Problem with IP
Post by: Nexy on September 20, 2010, 05:43:58 pm
I used about 30 port and nothing. They are reserved or what? How I can check free ports in my router?
Title: Problem with IP
Post by: Laurent on September 20, 2010, 06:01:27 pm
You need to open your own port, ports that are already opened are obviously already used as well.
Title: Problem with IP
Post by: Mindiell on September 21, 2010, 08:59:15 am
Have you a firewall ?
Can you modify the ports the modem let available ?
Title: Problem with IP
Post by: Nexy on September 21, 2010, 04:05:45 pm
Quote from: "Mindiell"
Have you a firewall ?
Can you modify the ports the modem let available ?


No, I don't have firewall and I don't know why it doesn't work.
Title: Problem with IP
Post by: Mindiell on September 22, 2010, 08:46:34 am
So, can you try the SFML network samples ? Is this working ?
Title: Problem with IP
Post by: Nexy on September 22, 2010, 03:00:10 pm
Quote from: "Mindiell"
So, can you try the SFML network samples ? Is this working ?


No :(
It's working only with "127.0.0.1", not "83.145.154.170".
Title: Problem with IP
Post by: Krosan on September 22, 2010, 03:38:57 pm
is the modem just a modem or is it a router?
your pc behind the modem has a private or public ip?

thanks..
Title: Problem with IP
Post by: Mindiell on September 22, 2010, 04:21:20 pm
Quote from: "Krosan"
your pc behind the modem has a private or public ip?
Under Windows : Execute : cmd
then ipconfig

Under Linux : ifconfig in a terminal ;)