I was working on a simple client/server test where a server sends a message to the client. The problem is that it'll work fine when I use localhost, but when me and my friend try it using each others' IPs it doesn't work. I am reading the IP and port from a text file. We are only changing the IP of the client since the server only uses the port. I also tried connecting to the server running on his machine with Telnet, but it still wouldn't work. We tried both the public and IPv4 IP addresses of our computers. What does it take to connect to a remote computer?
Client Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <SFML/Network.hpp>
int main()
{
std::fstream connection_info_file("AP.txt");
std::string server_ip;
std::string port;
std::getline(connection_info_file, server_ip);
std::getline(connection_info_file, port);
std::stringstream ip_buffer;
for(auto itr = server_ip.find("=") + 2; itr < server_ip.size(); itr++)
{
ip_buffer << server_ip.at(itr);
}
server_ip = ip_buffer.str();
std::stringstream port_buffer;
for(auto itr = port.find("=") + 2; itr < port.size(); itr++)
{
port_buffer << port.at(itr);
}
port = port_buffer.str();
std::cout << server_ip << std::endl;
std::cout << port << std::endl;
std::cout << "Welcome to the client." << std::endl;
sf::TcpSocket server;
sf::Socket::Status status = server.connect(server_ip, boost::lexical_cast<unsigned short>(port));
if(status != sf::Socket::Done)
{
std::cerr << "Error connecting." << std::endl;
return EXIT_FAILURE;
}
sf::Packet data;
server.receive(data);
std::string message;
if(data)
{
std::cout << "Getting data from: " << server.getRemoteAddress() << std::endl;
data >> message;
std::cout << message << std::endl;
}
else
{
std::cerr << "Data received not valid." << std::endl;
return EXIT_FAILURE;
}
sf::Clock delay;
while(delay.getElapsedTime().asMilliseconds() < 5000);
return EXIT_SUCCESS;
}
Server Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <SFML/Network.hpp>
int main()
{
std::fstream connection_info_file("AP.txt");
std::string server_ip;
std::string port;
std::getline(connection_info_file, server_ip);
std::getline(connection_info_file, port);
std::stringstream ip_buffer;
for(auto itr = server_ip.find("=") + 2; itr < server_ip.size(); itr++)
{
ip_buffer << server_ip.at(itr);
}
server_ip = ip_buffer.str();
std::stringstream port_buffer;
for(auto itr = port.find("=") + 2; itr < port.size(); itr++)
{
port_buffer << port.at(itr);
}
port = port_buffer.str();
std::cout << server_ip << std::endl;
std::cout << port << std::endl;
std::cout << "Welcome to the server." << std::endl;
sf::TcpListener listener;
if (listener.listen(boost::lexical_cast<unsigned short>(port)) != sf::Socket::Done)
{
std::cerr << "Error listening to socket." << std::endl;
return EXIT_FAILURE;
}
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
std::cerr << "Couldn't connect to client." << std::endl;
return EXIT_FAILURE;
}
else
{
sf::Packet data;
data << "Hello there.";
std::cout << "Connected to: " << client.getRemoteAddress() << std::endl;
client.send(data);
std::cout << "Data sent." << std::endl;
}
sf::Clock delay;
while(delay.getElapsedTime().asMilliseconds() < 5000);
return EXIT_SUCCESS;
}