1
Network / TCP doesn't work on remote computer
« on: June 11, 2021, 09:19:43 pm »
I created a simple application that sends a string to another machine. It works fine with both the client and the server connected to the same network, but it doesn't connect to a public address. Wireshark shows that the server is not responding if I send a request to the remote computer.
#include <SFML\Network.hpp>
#include <iostream>
int main()
{
sf::Packet packet;
char who;
std::cout << "Are you a server (s) or a client (c)?" << std::endl;
std::cin >> who;
if (who == 's')
{
sf::TcpSocket client;
std::cout << "Public: " << sf::IpAddress::getPublicAddress() << std::endl;
std::cout << "Local: " << sf::IpAddress::getLocalAddress() << std::endl;
sf::TcpListener listener;
if (listener.listen(3005) != sf::Socket::Done)
std::cout << "Could not bind port" << std::endl;
if (listener.accept(client) != sf::Socket::Done)
std::cout << "Could not connect to client" << std::endl;
client.receive(packet);
std::string name;
packet >> name;
std::cout << name << std::endl;
}
else if (who == 'c')
{
sf::TcpSocket server;
sf::IpAddress ip;
std::cin >> ip;
if (server.connect(ip, 3005) != sf::Socket::Done)
std::cout << "Could not connect to server" << std::endl;
packet << "Some random text";
server.send(packet);
std::cout << "Packet was sent" << std::endl;
}
system("pause");
}
#include <iostream>
int main()
{
sf::Packet packet;
char who;
std::cout << "Are you a server (s) or a client (c)?" << std::endl;
std::cin >> who;
if (who == 's')
{
sf::TcpSocket client;
std::cout << "Public: " << sf::IpAddress::getPublicAddress() << std::endl;
std::cout << "Local: " << sf::IpAddress::getLocalAddress() << std::endl;
sf::TcpListener listener;
if (listener.listen(3005) != sf::Socket::Done)
std::cout << "Could not bind port" << std::endl;
if (listener.accept(client) != sf::Socket::Done)
std::cout << "Could not connect to client" << std::endl;
client.receive(packet);
std::string name;
packet >> name;
std::cout << name << std::endl;
}
else if (who == 'c')
{
sf::TcpSocket server;
sf::IpAddress ip;
std::cin >> ip;
if (server.connect(ip, 3005) != sf::Socket::Done)
std::cout << "Could not connect to server" << std::endl;
packet << "Some random text";
server.send(packet);
std::cout << "Packet was sent" << std::endl;
}
system("pause");
}