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

Author Topic: Extracting Ip adress from a file  (Read 3349 times)

0 Members and 2 Guests are viewing this topic.

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Extracting Ip adress from a file
« on: March 12, 2015, 09:19:48 pm »
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;
}
« Last Edit: March 12, 2015, 11:00:20 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Extracting Ip adress from a file
« Reply #1 on: March 12, 2015, 10:06:53 pm »
Output IP and see if it's really what you'd expect.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: AW: Extracting Ip adress from a file
« Reply #2 on: March 12, 2015, 10:25:43 pm »
If I try to display that string as text, it displays correctly.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Extracting Ip adress from a file
« Reply #3 on: March 12, 2015, 10:47:14 pm »
Are you sure there aren't any unneeded spaces within the IP address or a new line character? Print it out in the loop and after it to see what you are really getting. :)
I have many ideas but need the help of others to find way to make use of them.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Extracting Ip adress from a file
« Reply #4 on: March 12, 2015, 11:02:50 pm »
Count the number of characters that you read, or print them as integers rather than characters, or anything else that makes 100% sure that the string really is what you expect. You may have a trailing \n or EOF.
Laurent Gomila - SFML developer

mihaineken

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Email
Re: Extracting Ip adress from a file
« Reply #5 on: March 14, 2015, 10:29:47 am »
Count the number of characters that you read, or print them as integers rather than characters, or anything else that makes 100% sure that the string really is what you expect. You may have a trailing \n or EOF.


It does work if you whrite the IP adress corectly in the file. :))
Stupid me !!
Sorry guys...