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

Author Topic: Problem with using TCP connecting  (Read 6677 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« 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.



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.



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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #1 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.
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #3 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.
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #4 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:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #5 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.
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #6 on: April 06, 2011, 01:35:42 pm »
Aha, ok and how do you do these things like this that they work please?  :roll:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #7 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 ;)
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #8 on: April 06, 2011, 02:20:04 pm »
So TCP and UDP is not work without setting up the router. :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #9 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.
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #10 on: April 06, 2011, 02:54:55 pm »
Can you give here link to topic what you was writing about please.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #11 on: April 06, 2011, 03:02:03 pm »
That's what the "Search" button is for ;)
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #12 on: April 07, 2011, 08:44:29 pm »
Can it be caused that I dont have a public IP?

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Problem with using TCP connecting
« Reply #13 on: April 09, 2011, 07:04:42 pm »
Please answer my question, I need to know it.   :roll:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Problem with using TCP connecting
« Reply #14 on: April 09, 2011, 07:40:08 pm »
I don't understand your question, actually.
Laurent Gomila - SFML developer

 

anything