SFML community forums

Help => Network => Topic started by: reDo on April 04, 2011, 03:11:08 pm

Title: Problem with using TCP connecting
Post by: reDo on April 04, 2011, 03:11:08 pm
I am learning network programming and I am trying examples from tutorial but I cannot understand how it cans work when it giving me a error message.

(http://i56.tinypic.com/25ps1hc.png)

And my second question is why it doesnt work when I write the IP from whatismyip.com as ServerAddress. I have Smart Security 4 and Windows Firewall on.

(http://i53.tinypic.com/4vmhsg.png)

Please explain it to me :roll: .
Server.cpp

Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    // Create a TCP socket for communicating with clients
    unsigned short Port = 4567;
    sf::SocketTCP Server;

    // Listen to a port for incoming connections
    if (!Server.Listen(Port))
        std::cout << "Error while starting listening\n";


    std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;

    // Wait for a connection
    sf::IPAddress ClientAddress("95.102.145.1");
    sf::SocketTCP Client;
    Server.Accept(Client, &ClientAddress);
    std::cout << "Client connected : " << ClientAddress << std::endl;

    // Send a message to the client
    char ToSend[] = "Hi, I'm the server";
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        std::cout << "Error while sending message to the client\n";

    std::cout << "Message sent to the client : \"" << ToSend << "\"" << std::endl;

    // Receive a message back from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        std::cout << "Error while receiving message from the client\n";

    // Show the message
    std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;

    // Close the sockets when we're done
    Client.Close();
    Server.Close();

    std::cin.get();

    return 0;
}


Client.cpp
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    // Ask for server address
    unsigned short Port = 4567;

    sf::IPAddress ServerAddress;

    do
    {
        std::cout << "Type address or name of the server to connect to : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());

    // Create a TCP socket for communicating with server
    sf::SocketTCP Client;

    // Connect to the specified server
    if (!Client.Connect(Port, ServerAddress))
        std::cout << "Error while connecting to server\n";

    std::cout << "Connected to server " << ServerAddress << std::endl;

    // Receive a message from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        std::cout << "Error while receiving message from the server\n";

    // Show it
    std::cout << "Message received from server : \"" << Message << "\"" << std::endl;

    // Define a message to send back to the server
    char ToSend[] = "Hi, I'm a client !";

    // Send the message
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        std::cout << "Error while sending message to the server\n";

    std::cout << "Message sent to server : \"" << ToSend << "\"" << std::endl;


    std::cin.get();
    std::cin.get();

    // Close the socket when we're done
    Client.Close();


    return 0;
}
Title: Problem with using TCP connecting
Post by: Laurent on April 04, 2011, 03:14:39 pm
Connect doesn't return a boolean, you must have read the doc/tutorial for an old version of SFML.

The problem with public IP is that the connection goes outside your private network through your router, which probably blocks the port that you use.
Title: Problem with using TCP connecting
Post by: reDo on April 04, 2011, 04:56:29 pm
Quote from: "Laurent"
Connect doesn't return a boolean, you must have read the doc/tutorial for an old version of SFML.


I only replaced return with error message and I am learning from SFML 1.6 tutorial.  :roll:

And the second answer. I tried it in Linux Mint 10 and it doesnt work also  :( and I havent set up any options.
Title: Problem with using TCP connecting
Post by: Laurent on April 04, 2011, 05:10:46 pm
Quote
I am learning from SFML 1.6 tutorial.

I don't think so:
Quote from: "http://www.sfml-dev.org/tutorials/1.6/network-sockets.php"
sf::SocketTCP Client;
if (Client.Connect(4567, "192.168.0.2") != sf::Socket::Done)
{
    // Error...
}


Quote
And the second answer. I tried it in Linux Mint 10 and it doesnt work also Sad and I havent set up any options.

You can try with all possible OSes that exist on earth, it won't work ;)
You need to setup your router so that it opens the port that you use, and redirects it to your PC.
Title: Problem with using TCP connecting
Post by: reDo on April 05, 2011, 07:23:23 pm
First, I only copied the source from tutorial :) but I think you are not true with setting up router because skype, bittorent and so on works good without setting up something. Can you help me with this please?  :roll:
Title: Problem with using TCP connecting
Post by: Laurent on April 05, 2011, 08:53:57 pm
Skype and bittorrent use uPnp, which means that they talk to the router directly without requiring you to manually open the ports.
Title: Problem with using TCP connecting
Post by: reDo on April 06, 2011, 01:35:42 pm
Aha, ok and how do you do these things like this that they work please?  :roll:
Title: Problem with using TCP connecting
Post by: Laurent on April 06, 2011, 01:46:29 pm
I don't know. But this topic has already been discussed in thisforum, as far as I remember. And you can still find plenty of useful information with Google of course ;)
Title: Problem with using TCP connecting
Post by: reDo on April 06, 2011, 02:20:04 pm
So TCP and UDP is not work without setting up the router. :(
Title: Problem with using TCP connecting
Post by: Laurent on April 06, 2011, 02:30:09 pm
Well, technically they can't work without this setup.

All machines that are behind a router have the same public IP address. Therefore, when the router receives a packet, it doesn't know where to forward it unless someone told it explicitely, based on the port.
Title: Problem with using TCP connecting
Post by: reDo on April 06, 2011, 02:54:55 pm
Can you give here link to topic what you was writing about please.
Title: Problem with using TCP connecting
Post by: Laurent on April 06, 2011, 03:02:03 pm
That's what the "Search" button is for ;)
Title: Problem with using TCP connecting
Post by: reDo on April 07, 2011, 08:44:29 pm
Can it be caused that I dont have a public IP?
Title: Problem with using TCP connecting
Post by: reDo on April 09, 2011, 07:04:42 pm
Please answer my question, I need to know it.   :roll:
Title: Problem with using TCP connecting
Post by: Laurent on April 09, 2011, 07:40:08 pm
I don't understand your question, actually.
Title: Problem with using TCP connecting
Post by: tokiwotomare on April 09, 2011, 08:27:38 pm
Quote
Can it be caused that I dont have a public IP?


If I'm understanding the question right -- no. If you're connected to the internet, you have a public (outward-facing) IP address. You said earlier:

Quote
And my second question is why it doesnt work when I write the IP from whatismyip.com as ServerAddress.


That IP is your public IP. What Laurent is saying is that if you intend to use that IP address to connect, you need to forward ports. What's essentially happening (in a very small nutshell) is this:

- Your computer issues a request for 178.41.66.24.
- Your router says "That's outside our network!" and sends it off to a DNS service.
- The DNS service sends the packet to 178.41.66.24, which, as it happens, is your router.
- Your router gets it and says "Okay, which machine does this go to?" To answer that question, it looks at what port it's coming in on and its port forwarding rules. Every router has port forwarding rules.
- You provide (I'm assuming) port 4567 as the port. Your router looks at its port forwarding rules and says "I don't have a rule for this." Since it doesn't know where to send it, it drops the packet (unless it has some default rule defined).

It's easier to use your local IP address for testing, because you (usually) don't have to configure your router at all. If you're running it from the same machine, you can use localhost (127.0.0.1) as the IP address. If you're targeting a different machine on your network, you'll need its local address, which you can find by typing "ipconfig" in your command prompt (or "ifconfig" if you're using a Linux-based OS).

If you really, really want to use your public IP address, you'll have to Google the instructions for setting up port forwarding with your router (or use UPNP, but I wouldn't suggest that until you learn some basic networking!).
Title: Problem with using TCP connecting
Post by: reDo on April 09, 2011, 09:28:40 pm
Thanks for this  :D I understand now