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

Author Topic: My Issue with TcpListener listen() method  (Read 232 times)

0 Members and 1 Guest are viewing this topic.

KRS

  • Newbie
  • *
  • Posts: 15
  • The burning you feel?
    • View Profile
    • Email
My Issue with TcpListener listen() method
« on: March 24, 2021, 03:07:05 pm »
Hello! I'll be happy if someone will share their thought with me about this
Basically when
listen(unsigned short port);
is called like this
class C
{
        TcpListener l;
public:
        C() { l.listen(53000); };
};

C c;

int main()
{
        return 0;
}      
it prints in console
Failed to create socket

but if it's called like this
class C
{
        TcpListener l;
public:
        C() { l.listen(53000); };
};

int main()
{
        C c;
        return 0;
}
it's all good. Only difference I'm spotting is that in second variant, call is inside the main(). I want to know why is that happening, maybe I'm missing something. And is it connected with location like I noticed?

Kvaz1r

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Re: My Issue with TcpListener listen() method
« Reply #1 on: March 27, 2021, 09:23:18 am »
I think the reason in Zero initialization. Here is blog post with more human explanation - Initialization in C++ is bonkers.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9795
    • View Profile
    • development blog
    • Email
Re: My Issue with TcpListener listen() method
« Reply #2 on: March 29, 2021, 01:11:19 pm »
You'd have to trace the code to see what state isn't necessarily lining up.

But using global variables is highly discouraged and not really supported by SFML.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6254
  • Thor Developer
    • View Profile
    • Bromeon
Re: My Issue with TcpListener listen() method
« Reply #3 on: April 11, 2021, 04:56:53 pm »
A bit late, but the reason is not zero initialization -- both times, the same constructor is called, default-constructing the sf::TcpListener member variable.

As expl0it3r mentioned, initialization at startup (i.e. from global variables) is a big mess in C++, as there's no deterministic order. So better avoid global variables in general, good design almost always makes them obsolete.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book