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

Author Topic: Cannot connect to a remote IP with TCP  (Read 6132 times)

0 Members and 1 Guest are viewing this topic.

Alkanor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Cannot connect to a remote IP with TCP
« on: April 16, 2018, 12:58:34 pm »
Hi  :)

My problem is quite simple but I haven't found answers on other topics.

I'm trying to connect 2 computers (on different networks) using the public IP of the host :

int main()
{
    std::cout << "Will you host? (y/n)" << endl;

    char answer;
    std::cin >> answer;

    if (answer == 'y')
    {
        // Server side.
        sf::IpAddress ip = sf::IpAddress::getPublicAddress(sf::seconds(3));

        cout << "Server address : " << ip << endl;    // Displays "176.182.85.44" (dummy).
       
        sf::TcpListener listener;
        if (listener.listen(53000) != sf::Socket::Done)
        {
            cout << "Error listening port 53000" << endl;
            return 1;
        }

        cout << "Waiting for clients..." << endl;

        sf::TcpSocket client;
        if (listener.accept(client) != sf::Socket::Done)
        {
            cout << "Client not accepted" << endl;
            return 1;
        }

        cout << client.getRemoteAddress() << " connected" << endl;
    }
    else
    {
        // Client side.
        sf::TcpSocket server;

        std::cout << "Connection to the server..." << endl;

        sf::TcpSocket::Status status = server.connect("176.182.85.44", 53000, sf::seconds(3)); // Always returns sf::Socket::Error.
        if (status != sf::Socket::Done)
        {
            std::cout << "Connection failed" << endl;
            server.disconnect();
            return 1;
        }
       
        std::cout << "Connection to " << server.getRemoteAddress() << " succeed" << endl;
    }

    return 0;
}
 

If I run this code on my local network (using 192.168.*.* or 127.0.0.1), it works perfectly.
But impossible to connect 2 computers over the network, even when I turn off the firewall on both sides !
Indeed, on the client I always get a "Connection failed" (server.connect() always returns sf::Socket::Error).
On the server side no connection is detected, it sticks to "Waiting for clients..."

I don't know if it is ISP related or anything else. Maybe somebody could help me? Thanks a lot ;D
« Last Edit: April 16, 2018, 04:05:37 pm by Alkanor »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Cannot connect to a remote IP with TCP
« Reply #1 on: April 17, 2018, 08:01:31 am »
Have you setup port fowarding?

Also check the FAQ: https://www.sfml-dev.org/faq.php#network-internet-network
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Alkanor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Cannot connect to a remote IP with TCP
« Reply #2 on: April 17, 2018, 09:28:37 am »
Thank you for the reply.

I've read the FAQ but I don't understand a lot what I have to do about port forwarding.
If my server is behind a NAT (which is currently the case), have I to port forwarding the server by reaching it with UDP packets from the client?