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

Author Topic: IpAddress turns into "0.0.0.0" in vector?  (Read 3856 times)

0 Members and 1 Guest are viewing this topic.

ppsychrite

  • Newbie
  • *
  • Posts: 6
    • View Profile
IpAddress turns into "0.0.0.0" in vector?
« on: April 07, 2017, 02:48:09 am »
I've been using Udp and trying to find a way to handle cilents.
Right now I'm just using a vector of ip addresses to hold ips that send stuff (std::vector<sf::IpAddress> cilents)

(Snippet of code)
if(socket.receive(packet, new_cilent, port) != sf::Socket::NotReady){
    std::cout << new_cilent << std::endl; //<-- This prints out actual ip address
    if(cilents.empty()){
        cilents.push_back(new_cilent);
        std::cout << cilents[0] << std::endl; //<-- This prints out "0.0.0.0"
    }

 

Are ip addresses unable to work with vectors or was it something wrong on my end?
Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #1 on: April 07, 2017, 06:29:53 am »
You should also print cilents.size() (maybe the address you push to the array is not at position 0 for some reason).

And it's client, not cilent ;D
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #2 on: April 07, 2017, 09:17:43 am »
With that if empty it'll be at index 0 surely... that's very unusual.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #3 on: April 07, 2017, 09:38:28 am »
Oops... I missed the "if empty" condition, indeed.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #4 on: April 07, 2017, 11:04:27 am »
Does this example work for you?

#include <SFML/Network.hpp>
#include <vector>
#include <iostream>

int main()
{
        std::vector<sf::IpAddress> clients;
        sf::IpAddress test(10, 0, 0, 1);
        std::cout << test << "\n";
        clients.push_back(test);
        std::cout << clients.back() << "\n";
}

It seems to run fine on my end.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ppsychrite

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #5 on: April 07, 2017, 02:06:57 pm »
You should also print cilents.size() (maybe the address you push to the array is not at position 0 for some reason).

And it's client, not cilent ;D
It prints out a size of "1" right after the push-back
Oh I always thought it was "cilent"  :P

Does this example work for you?

#include <SFML/Network.hpp>
#include <vector>
#include <iostream>

int main()
{
        std::vector<sf::IpAddress> clients;
        sf::IpAddress test(10, 0, 0, 1);
        std::cout << test << "\n";
        clients.push_back(test);
        std::cout << clients.back() << "\n";
}

It seems to run fine on my end.

That's strange, they both print out "10.0.0.1" fine
(If it helps, I also tried the "back()" method instead of "[ 0 ]" and that returns 0.0.0.0, too)
« Last Edit: April 07, 2017, 02:14:59 pm by ppsychrite »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #6 on: April 07, 2017, 02:49:20 pm »
Did you create a new project to test my code?

I recommend you check your project settings, to make sure you're not mixing debug and release modes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ppsychrite

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: IpAddress turns into "0.0.0.0" in vector?
« Reply #7 on: April 07, 2017, 02:57:23 pm »
Did you create a new project to test my code?

I recommend you check your project settings, to make sure you're not mixing debug and release modes.
Yes, I created a new project with the same project settings to test the code.
(It's compiled using the way to compile .cpp programs on linux with command-line)