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

Author Topic: Network code that only works on Local Host...  (Read 4304 times)

0 Members and 1 Guest are viewing this topic.

s0501402

  • Newbie
  • *
  • Posts: 4
    • View Profile
Network code that only works on Local Host...
« on: March 16, 2018, 10:01:04 pm »
I am new to C++ and SFML. So I am playing with TCP recently and found out that the code I wrote only works in Local Host but not internet.
At first I thought there is something wrong in my code but after testing the TCP Example code from the documentation, it still seems like only works in local host.
It will be nice if someone can point out what I did wrong or test it out too.

Below is the slightly modified code from the official documentation:
https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1TcpSocket.php

#include <iostream>
#include<SFML/Network.hpp>

int main()
{
    std::cout << "Client[c] or server[s]? " << std::endl;
    std::string consoleInput;
    std::getline(std::cin, consoleInput);
    if(consoleInput == "c")
    {
        std::cout << "Input server address: " << std::endl;
        std::getline(std::cin, consoleInput);

        // ----- The client -----
        // Create a socket and connect it to port 55001
        sf::TcpSocket socket;
        socket.connect(consoleInput, 55001);

        // Send a message to the connected host
        std::string message = "Hi, I am a client";
        socket.send(message.c_str(), message.size() + 1);

        // Receive an answer from the server
        char buffer[1024];
        std::size_t received = 0;
        socket.receive(buffer, sizeof(buffer), received);
        std::cout << "The server said: " << buffer << std::endl;
        std::cout << "Press any key to close" << std::endl;
        std::cin >> message;
    }
    else if(consoleInput == "s")
    {
        // ----- The server -----
        // Create a listener to wait for incoming connections on port 55001
        sf::TcpListener listener;
        listener.listen(55001);

        //Show ip address
        std::cout << sf::IpAddress::getLocalAddress() << std::endl;
        std::cout << sf::IpAddress::getPublicAddress() << std::endl;

        // Wait for a connection
        sf::TcpSocket socket;
        listener.accept(socket);
        std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl;

        // Receive a message from the client
        char buffer[1024];
        std::size_t received = 0;
        socket.receive(buffer, sizeof(buffer), received);
        std::cout << "The client said: " << buffer << std::endl;

        // Send an answer
        std::string message = "Welcome, client";
        socket.send(message.c_str(), message.size() + 1);

        std::cout << "Press any key to close" << std::endl;
        std::cin >> message;
    }

    return 0;
}
 


When testing this code in local host, it does fine: (Untitled.png)


But when it comes to testing with remote devices: (unknown.png)

One of my friend got gibberish when he connects to me and I can't even connect to him. Note that firewall and anti-virus has been turned off.



Edit:
Obviously when I use this example, there is no status return. But when I use my own code, the server hosts successfully but the status returned on the client side when sending is "Not Ready".


Edit 2:
Got it, its just port forwarding. Didn't know it needs to be done before hosting server.

« Last Edit: March 17, 2018, 12:15:18 am by s0501402 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Network code that only works on Local Host...
« Reply #1 on: March 16, 2018, 10:09:18 pm »
One possible problem: https://en.wikipedia.org/wiki/Endianness . You have to make sure your client and server use the same Endianness or otherwise you have to convert the received/sent data. Maybe you or your friend is using a old mac (power pc)?

Better: just use sf::Packet which takes care of this ;)


AlexAUT

s0501402

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Network code that only works on Local Host...
« Reply #2 on: March 16, 2018, 10:17:58 pm »
Possible, but considering we are both using Windows 10 64 bit. It is a bit unlikely for that to happen. Still, even if we are in different Endiannes, the console should still shows Client or Server connected. But in this case nothing happens.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Network code that only works on Local Host...
« Reply #3 on: March 16, 2018, 10:28:54 pm »
But he got some content, just not the expected string? Or do I missunderstand your images?  ???

Have you checked the status of the sockets? (return value of listen/accept/connect/receive/send)


AlexAUT

s0501402

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Network code that only works on Local Host...
« Reply #4 on: March 16, 2018, 11:20:06 pm »
Yes, you are right he got some content, but on the server I didn't receive any message nor stating that anyone has connected. That can be reproduced when running client and server on the same device, by connecting to the public address instead of the local address. Do not know why is that happening though.

Also, there are status returned by using my code, which I have put onto the edit part of the post.
« Last Edit: March 16, 2018, 11:21:39 pm by s0501402 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Network code that only works on Local Host...
« Reply #5 on: March 17, 2018, 03:03:06 am »
Always a good idea to check the FAQs: https://www.sfml-dev.org/faq.php#network-internet-network

And then start up Wireshark and see what data is being sent around.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

s0501402

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Network code that only works on Local Host...
« Reply #6 on: March 17, 2018, 04:52:26 pm »
Thanks for the heads up. Will do next time  :)