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!