golightlydev
golightlydev

Reputation: 55

C++ SFML Networking: Can't Establish a TCP Connection

Second Update: ok, here's the wireshark results for what happens when I try to connect to a friend with this program, and it fails. I've tried to remove his info, that's all the redacted lines.

Update: This works for all the computers on my local network, using local ip addresses. It's when I try to connect to a remote computer with a public ip address that it has problems, now. The client gets "cannot connect", after about 20 seconds, followed by "error number 3", where 3 is the error code provided by sfml.

I'd just like to know how to establish a remote connection. I've looked at several other examples which seem to be identical to mine, and they work.

edit: I forwarded port 5000 to my computer on my router and gave that port to the client, along with my public ip address, in case if that was unclear. Here's a screenshot of the rule I set up http://i.imgur.com/Q8Kyu1E.png. I also tried disabling the windows firewall entirely, with the same results.

Here are the relevant parts of the code:

#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <vector>
#include <string>
#include "font.h"
#include "overhead.h"
#include "texture.h"
#include <iostream>

void render(Overhead &overhead, SDL_Rect *clip, Texture &texture, double angle, SDL_Point *centre, SDL_RendererFlip flip) {
    SDL_Rect render = { texture.textData.x, texture.textData.y, texture.textData.w, texture.textData.h };
    SDL_RenderCopyEx(overhead.renderer, texture.texture, clip, &render, angle, centre, flip);
}

void close(Overhead &overhead, Font &font) {
    TTF_CloseFont(font.font);
    font.font = NULL;
    overhead.closeOverhead();
}

int main(int argc, char* args[]) {
    sf::Packet packet;
    sf::TcpSocket peer;
    sf::TcpListener listener;
    unsigned short port;
    std::string ip;
    sf::IpAddress ipaddress;
    char host;
    std::cout << "host (y/n): ";
    std::cin >> host;
    if (host == 'y') {
        std::cout << "port number to listen on: ";
        std::cin >> port;
        std::cout << "IP Address for local connection: " << sf::IpAddress::getLocalAddress() << std::endl;
        std::cout << "IP Address for remote connection: " << sf::IpAddress::getPublicAddress() << std::endl;
        if (listener.listen(port) != sf::Socket::Done)
            std::cout << "listener error" << std::endl;
        if (listener.accept(peer) == sf::Socket::Done)
            std::cout << "connection successful" << peer.getRemoteAddress() << std::endl;
    }
    else {
        std::cout << "port number to connect to: ";
        std::cin >> port;
        std::cout << "you chose port number " << port << std::endl;
        std::cout << "ip address to connect to: ";
        std::cin >> ip;
        ipaddress = ip;
        sf::Socket::Status status = peer.connect(ipaddress, port);
        if (status != sf::Socket::Done) {
            std::cout << "cannot connect" << std::endl;
            std::cout << "error number: " << status;
        }
        else if (status == sf::Socket::Done)
            std::cout << "connection successful" << std::endl;
    }
    //other stuff
    peer.disconnect();
    close(overhead, font);
    return 0;
}

Upvotes: 0

Views: 901

Answers (1)

J-M. Gorius
J-M. Gorius

Reputation: 786

I have had the exact same issue while trying to host a server on my local network. It seems that, just like mine, your router does not allow NAT Loopback, i.e. access to your external IP via the local network (for more details about NAT Loopback, see this link).
In fact, you will be able to access the host from a client located outside of your local network using your public IP address, but you will have to use your local address if you want to access it from inside the local network.

Upvotes: 1

Related Questions