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

Author Topic: [SOLVED] Problem with TcpListener  (Read 3838 times)

0 Members and 1 Guest are viewing this topic.

filipekczek7

  • Guest
[SOLVED] Problem with TcpListener
« on: May 28, 2016, 12:21:31 pm »
(In the beginning, sorry for my english ;))

I'm new on this forum, so I would like to greet you all :)

OK, I have some experience with SFML. About 3 months ago I became interested in sockets (and then threads). I have made simple chat with sockets (with port forwarding)... And it works, yey, it's so easy and I understand it :) So now I want to make some network game. The first step is listen to connection. OK, here is some code:



Function which displays text and launching listening thread:
void Game::playOnlineServerMenu()
{
    //setting texts
        int textsNumber=3;
        Text texts[textsNumber];
    //string strings[textsNumber]={"Play","Options","Credits","Exit"};
        for(int i=0;i<textsNumber;i++)
        {
                texts[i].setFont(font);
                texts[i].setCharacterSize(150);
        if(i==0)
            texts[i].setString("Waiting for connection...");
        else if(i==1)
        {
            string ip="Your IP is: "+IpAddress::getPublicAddress().toString();
            texts[i].setString(ip);
        }

                if(i<1)
            texts[i].setPosition(window.getSize().x/2-texts[i].getGlobalBounds().width/2,250+i*120);
        else if(i==1)
            texts[i].setPosition(window.getSize().x/2-texts[i].getGlobalBounds().width/2,250+i*120);

        texts[i].setColor(Color(175,175,175));
        }

    //listening to client
        thread.launch();

    //main loop
        while(menuState==playOnlineServer)
        {
            cout << "Main loop" << endl;

                Event event;

                while(window.pollEvent(event))
                {
                    //pressed X button
                        if(event.type==Event::Closed)
            {
                thread.terminate();
                menuState=exit;
            }
                        //released escape button
                        if(event.type==Event::KeyPressed&&event.key.code==Keyboard::Escape)
            {
                thread.terminate();
                menuState=menu;
            }
                }

        window.setMouseCursorVisible(true);
        window.setView(window.getDefaultView());
                window.clear();
                for(int i=0;i<textsNumber;i++)
            window.draw(texts[i]);
                window.display();
        }
}

Listening and accepting connection function:
void Game::listening()
{
    Lock lock(mutex);

    cout << "Thread" << endl;

    //listening to connection
    if(listener.listen(port)!=Socket::Done)
        cout << "!ERROR! Server didn't find any connection." << endl;
    else
        cout << "Connection found" << endl;
    //accepting connection
    if(listener.accept(socket)!=Socket::Done)
        cout << "!ERROR! Server cannot accept connection." << endl;
    else
    {
        cout << "Connection accepted" << endl;
        listener.close();
        menuState=gameOnlineServer;
    }
}

Thread is class member, so it's initialized correctly:
Game::Game():thread(&Game::listening,this)
{
    //...
}



I'm gonna transform my code to something better (polymorphism, virtual functions and generally better code design), but it has to work.

OK, so I choose "Server" option, then I see my IP and some text, then I press Escape key and leave whole program. That's what I see in the console output:

Main loop
Thread
Connection found
Main loop
Main loop
Main loop
Main loop
Main loop
...


What?! Listener immediately found connection? :o But there's not "!ERROR! Server cannot accept connection." or "Connection accepted". I don't understand. I removed thread and just called listening function after draw everything:

Main loop
Thread
Connection found


I don't know what's going on :/ Situation didn't change, so it's not thread fault. My previous network programs work, so I don't know what's wrong with this :/ I solved previous problems, but this is problem with SFML sockets, so I have no idea what's wrong.

Thanks for help.
« Last Edit: May 28, 2016, 03:12:07 pm by filipekczek7 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with TcpListener
« Reply #1 on: May 28, 2016, 01:40:26 pm »
TcpListener::listen just... listens. It doesn't mean that there is a connection. Accepting connections is done with (surprisingly) TcpListener::accept ;)

So there's no problem in your program. Don't hesitate to read the documentation / tutorials again to make things clear.
Laurent Gomila - SFML developer

filipekczek7

  • Guest
Re: Problem with TcpListener
« Reply #2 on: May 28, 2016, 02:25:39 pm »
Yes, I know what listen() and accept() functions do :) And yes, there's no problem in my program. No, sorry, there is a problem, but I can't see it :| Doesn't listener call accept() function only when listen() found something? You know:

listener is listening...
listener is accepting connection, because it found something, some connection

Why listener stops listening after first approach? I don't understand... No, wait. So... listener.listen(port) is Socket::Done when it's listening? Not only when it found some connection?

filipekczek7

  • Guest
Re: Problem with TcpListener
« Reply #3 on: May 28, 2016, 02:34:11 pm »
THANKS!!! OMG!!! You're genius! Or even if you are not genius, I'm very stupid :D Thanks for some knowledge, I'll remember that. I thought "Hm, if listen() is Socket::Done when it's listening... Wait... Oh, come on! I didn't remove comment from the code fragment in other file!" :D No comment BADUM-TSS! yeah, I know, not funny. Small bug, but after fix repairs everything.

Thank you for me to realize that it works :) OK, the topis is closed, my mistake :) Is there possibility to close topic?

PS: thank you for creating SFML ;)
« Last Edit: May 28, 2016, 03:52:38 pm by filipekczek7 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with TcpListener
« Reply #4 on: May 28, 2016, 03:03:22 pm »
Is there possibility to close topic?
Just rename the Subject of the thread in the original post so that it starts with [SOLVED]
i.e. "[SOLVED] Problem with TcpListener"
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

filipekczek7

  • Guest
Re: Problem with TcpListener
« Reply #5 on: May 28, 2016, 03:11:44 pm »
Oh, OK, thanks :)

 

anything