I have made a client that needs to connect to an IP adress that is stored in a file.
The file contains only the IP adress.
I have extracted the IP adress character by character and store it into a std::string.
The string is passed in the connecting function and doesn't work.
Here is the code :
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <fstream>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
sf::RenderWindow window(sf::VideoMode(800, 400), "The Game" );
sf::Font font ;
sf::Text text ;
std::string IP ; //The string that will be passed as the IP adress
std::ifstream file_in( "IP.txt" ); //The file that contains the IP adress
char input_line ;
sf::TcpSocket socket_client ;
font.loadFromFile( "Sansation.ttf" );
text.setFont( font );
text.setCharacterSize( 20 );
text.setColor( sf::Color::White );
while( file_in.get( input_line )) //Extracting char from the line in the file
{
IP += input_line ;
}
file_in.close() ;
sf::Socket::Status status_client = socket_client.connect( IP , 31111 );
//In the line above, if I pass the IP adress as "192.168.10.10" instead of IP, it will connect
if( status_client == sf::Socket::Done )
{
text.setString( "Connected" );
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw( text );
window.display();
}
return 0;
}