SFML community forums

Help => Network => Topic started by: beegyfleeg on July 19, 2020, 05:23:29 am

Title: Simple Network test program not working (newbie)
Post by: beegyfleeg on July 19, 2020, 05:23:29 am
This is a simple program I wrote to test the functionality of the network library. I just need to know if it's my code that's the issue, or if there's any device and network business that's preventing it from working.
#include <iostream>
#include <SFML/Network.hpp>
using namespace std;
using namespace sf;

TcpListener listener; // host socket that listens for new connections
TcpSocket socket; // client to host communication socket

int main()
{
        cout << "Host or client?\n"
                << "A - Host\n"
                << "B - Client\n";
        char input;
        cin >> input;
        if (input < 'a') input += 'a' - 'A';
        if (input == 'a') {
                cout << "Your IP:" << IpAddress::getPublicAddress().toString() << endl;
                listener.listen(31415);
                listener.accept(socket);
                Packet packet;
                socket.receive(packet);
                char message[40];
                packet >> message;
                cout << message;
        }
        else {
                char ip[16];
                cout << "Enter host IP:\n";
                cin >> ip;
                if (socket.connect(ip, 31415, seconds(5)) == Socket::Status::Done) {
                        cout << "Send a message:\n";
                        char message[40];
                        cin.getline(message, 40);
                        cin.getline(message, 40);
                        Packet packet;
                        packet << message;
                        socket.send(packet);
                }
                else {
                        cout << "No connection available.\n";
                }
        }
        char message[40];
        cin.getline(message, 40);
        cin.getline(message, 40);
}
 

Any feedback is welcome. Thanks! :)
Title: Re: Simple Network test program not working (newbie)
Post by: eXpl0it3r on July 23, 2020, 08:11:27 am
I suggest checking out the network example in the source code of SFML and read the network tutorial.
You should be checking return values and have loops that keep trying to receive/send etc.

As for anything networking, I highly recommend getting yourself familiar with Wireshark and alike, so you can check whatever the application is actually communicating, compared to what you expect it's doing.