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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - s0501402

Pages: [1]
1
Network / Re: Network code that only works on Local Host...
« on: March 17, 2018, 04:52:26 pm »
Thanks for the heads up. Will do next time  :)

2
Network / Re: Network code that only works on Local Host...
« 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.

3
Network / Re: Network code that only works on Local Host...
« 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.

4
Network / 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.


Pages: [1]
anything