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

Author Topic: Simple Network test program not working (newbie)  (Read 4295 times)

0 Members and 1 Guest are viewing this topic.

beegyfleeg

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Simple Network test program not working (newbie)
« 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! :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Simple Network test program not working (newbie)
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/