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

Author Topic: connecting externally  (Read 4308 times)

0 Members and 3 Guests are viewing this topic.

rgrant1993

  • Newbie
  • *
  • Posts: 12
    • View Profile
connecting externally
« on: August 03, 2013, 01:13:47 am »
I have been at this for hours now -.- So annoyed..

I have my server set to listen on port 2000 like sooo..
sf::TcpListener listener;
listener.listen(2000);
listener.accept(socket);
 

Then i have my client set to connect to the server like so...
socket.connect("142.197.200.18", 2000);
 

Why doesn't this work? I need to do this so that people not on my network could connect.. Doing this locally is unacceptable.

Quote
Entire Code:
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<iostream>
#include<stdlib.h>
#include<ctime>
 
int main()
{
    sf::IpAddress ip = "142.197.200.18";
        std::cout << ip;
    sf::TcpSocket socket;
    char connectionType;
    std::string text = "";
    char buffer[2000], mode = 'r';
    std::size_t received;
 
    std::cout << "Enter (s) for Server, Enter (c) for Client: ";
    std::cin >> connectionType;
 
    if(connectionType == 's')
    {
        sf::TcpListener listener;
        listener.listen(2000);
        listener.accept(socket);
        text += "Server";
        mode = 's';
    }
    else if(connectionType == 'c')
    {
        socket.connect(ip, 2000);
        text += "Client";
    }
 
    socket.send(text.c_str(), text.length() + 1);
    socket.receive(buffer, sizeof(buffer), received);
 
    std::cout << buffer << std::endl;
     
    bool done = false;
 
    while(!done)
    {
        if(mode == 's')
        {
            std::getline(std::cin, text);
            socket.send(text.c_str(), text.length() + 1);
            mode = 'r';
        }
        else if(mode == 'r')
        {
            socket.receive(buffer, sizeof(buffer), received);
            if(received > 0)
            {
                std::cout << "Received: " << buffer << std::endl;
                mode = 's';
            }
        }
    }
    return 0;
}
 
« Last Edit: August 03, 2013, 12:32:50 pm by Laurent »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [URGENT] connecting externally
« Reply #1 on: August 03, 2013, 05:01:24 am »
First, connecting to your own public IP from inside your public IP will not work. You will need an external internet connection to test connecting in. Second, if you are using a fixed IP address you need to make sure that your ISP has given you a static IP. On top of having a static IP you also need to make sure your router/firewall is setup with port forwarding.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

rgrant1993

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: connecting externally
« Reply #2 on: August 03, 2013, 03:47:13 pm »
I was wondering about that. Are you 100% sure about that?
I suppose that my next question would be considered off topic so ill make a new thread :) thank you
« Last Edit: August 03, 2013, 04:03:07 pm by rgrant1993 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: connecting externally
« Reply #3 on: August 03, 2013, 06:07:15 pm »
I was wondering about that. Are you 100% sure about that?

100% sure about not being able to connect to your own public IP? Yes I am 100% sure about that, trying to do that is like trying to call yourself using the same phone you are calling with.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: connecting externally
« Reply #4 on: August 03, 2013, 07:02:03 pm »
You can connect to yourself using your public IP, but you have to configure your router and firewall so that they don't block the connection.

Quote
100% sure about not being able to connect to your own public IP? Yes I am 100% sure about that, trying to do that is like trying to call yourself using the same phone you are calling with
Hmm... not exactly ???
Laurent Gomila - SFML developer

rgrant1993

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: connecting externally
« Reply #5 on: August 03, 2013, 07:30:58 pm »
You can connect to yourself using your public IP, but you have to configure your router and firewall so that they don't block the connection.

;) lets drop that suggestion from future suggestions. it's quite obvious :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: connecting externally
« Reply #6 on: August 03, 2013, 07:52:31 pm »
Quote
it's quite obvious
You would be suprised to see how many developers fail on this point.
Laurent Gomila - SFML developer