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

Author Topic: [Solved] Thread & Multiplayer  (Read 3252 times)

0 Members and 1 Guest are viewing this topic.

Flutschi

  • Newbie
  • *
  • Posts: 4
    • View Profile
[Solved] Thread & Multiplayer
« on: November 19, 2012, 03:57:52 pm »
Hi there,


Im trying to connect to localhost with the Listening part running in a seperate thread... but it wont connect... and i dont know why :/

I tried many differend setting for IP, like "127.0.0.1" or "192.168.1.34" (Thats my local IP) but it wont connect..


The Idea behind is that i wanted to create a small game for multiple users, and one of them is the Host and he listens so users can connect...


My thought was that the connect is "too fast" so i put it into a loop so he connects over and over when he fails, but he never get the connection even then :/

        lthread = new sf::Thread(&Host::Listen, this);
        lthread->launch();
 

void Host::Listen()
{

        sf::TcpListener Listener;
        Listener.listen(1234);

        quit = false;

        while (!quit)
        {
       
                sf::TcpSocket *client = new sf::TcpSocket;
                Listener.accept(*client);
               
        }

}

if ((clientSocket.connect("127.0.0.1", 1234) == sf::Socket::Done))
        {
                connected = true;
        }
 
« Last Edit: November 19, 2012, 11:31:48 pm by Flutschi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Thread & Multiplayer
« Reply #1 on: November 19, 2012, 04:23:23 pm »
Can you please show a complete and minimal code that reproduces the problem?
Laurent Gomila - SFML developer

Flutschi

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread & Multiplayer
« Reply #2 on: November 19, 2012, 06:22:57 pm »
There is not much more code as posted above, what else you want to know? Im currently at my cellphone so my writing abilities are reduced ;)

It creates the thread, launches it and then the starterprocess wants to connect right afte...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

Flutschi

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread & Multiplayer
« Reply #4 on: November 19, 2012, 09:35:08 pm »
Ok, i dont know what else to post you tbh, this is where its never connecting...

Host.cpp

void Host::Listen()
{

        sf::TcpListener Listener;
        sf::IpAddress MyIP;

        Listener.listen(1234);
        cout << "Local: " << MyIP.getLocalAddress().toString() << "   ::   Public: " << MyIP.getPublicAddress().toString() << endl;

        quit = false;

        while (!quit)
        {
       
                sf::Packet send;
                string s;

                sf::TcpSocket *client = new sf::TcpSocket;
                cout << "Waiting for connection.." << endl;
       
                Listener.accept(*client);
                clients.push_back(client);
               
        }

}

void Host::Start()
{

        lthread = new sf::Thread(&Host::Listen, this);
        lthread->launch();

        Client client("127.0.0.1");
        client.Start();


}


Client.cpp

bool Client::Connect(string IP)
{

        bool connected = false;

        if ((clientSocket.connect(IP, 1234) == sf::Socket::Done)) /// This here NEVER connects... dont know why
        {
                cout << "Connected" << endl;
                connected = true;
        }
        if (connected == false)
        {
                cout << "Failed.." << endl;
                Connect(IP);
        }

        return connected;

}

void Client::Start()
{


        if (MyIP == "")
        {
                cout << "What IP?" << endl;
                cin >> MyIP;
        }

        Connect(MyIP);

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Thread & Multiplayer
« Reply #5 on: November 19, 2012, 10:47:45 pm »
Quote
Ok, i dont know what else to post you tbh
Did you read my link carefully?

Quote
So what you must do is to extract the relevant lines of code from your application and write a new piece of code that contains only them. We don't care if the new code doesn't do exactly what your original application does, all it has to do is to reproduce the problem.
Quote
It is important that:
  • the code is complete, so people can test it
  • the code is as simple as possible, the best is one main() with all the code inside -- people don't want to setup a complex project just to test your code
Quote
While reducing the code, chances are that you solve the problem yourself: things are clearer and more obvious with less code. And if you can't find anything, we will most likely give you an answer very quickly because the code to examine is very small.

I don't mean to bother you, it's just how people usually solve bugs. There's no magic, people that answer on forums are just like you, except that they don't have your code and can't spend hours staring at it to try to find an obvious error. The minimal example allows to focus on the problem, and to quickly copy-compile-test-debug it.
« Last Edit: November 19, 2012, 10:50:53 pm by Laurent »
Laurent Gomila - SFML developer

Flutschi

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread & Multiplayer
« Reply #6 on: November 19, 2012, 11:15:31 pm »
Edit2:

Ok, figured it out... somehow this line (in my constructor..)

clientSocket.setBlocking(false);

made it somehow connect before i even called the clientSocket.connect function... well well who would have guessed :D
« Last Edit: November 19, 2012, 11:32:21 pm by Flutschi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Solved] Thread & Multiplayer
« Reply #7 on: November 22, 2012, 08:05:03 am »
Quote
made it somehow connect before i even called the clientSocket.connect function
Hmmm... no ;)
But non-blocking connection is not reliable, it doesn't work well on all systems and needs to be improved.

Quote
well who would have guessed
Nobody, especially since this line was not shown in your first post.

Now I hope that you understand better why the complete/minimal code is necessary sometimes.
Laurent Gomila - SFML developer

 

anything