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

Author Topic: How to make my program wait for establishing the connection?  (Read 3328 times)

0 Members and 1 Guest are viewing this topic.

Derp

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to make my program wait for establishing the connection?
« on: December 07, 2011, 02:59:53 pm »
Hello.
My SFML-Network works fine on LAN but there is a problem connecting to remote host on the Internet. It seems that my code trying to send a packet immediately after the connection attempt (program needs more time to connect).
My code:
Code: [Select]
if(Client.Connect(7777, host)!=Socket::Done){
  std::cout<<"Connection Error"<<std::endl;
}

Next goes the sending the first packet.
There is no problem with this code (print to 5000 delays my app for 3-4 seconds):
Code: [Select]
   if(Client.Connect(7777, host)){
       while(i<5000){
           std::cout<<i<<std::endl;
           i++;
       }
    }

How do I delay my program for the time needed to connect to remote host?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to make my program wait for establishing the connection?
« Reply #1 on: December 07, 2011, 05:05:48 pm »
Hum, you should be able to send a message right after Connect returns, since it's blocking. Have you tried? Does it fail?
Laurent Gomila - SFML developer

Derp

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to make my program wait for establishing the connection?
« Reply #2 on: December 07, 2011, 08:40:30 pm »
Thanks for reply.

Well, this is what happens when I run code 1:
I press "Login".
Nothing happens but my server says that client has connected.
I press "Login" one more time.
The packet with username and password is delivered successfully.

and with code 2:
I press "Login".
Server says that client has connected.
3-4 seconds passess while counting to 5000.
The packet is delivered.

or you suggested to make something like while(client.connect(port,host)!=Socket::Done) waiting loop?
please forgive my noobing :roll:

nitrix

  • Newbie
  • *
  • Posts: 27
    • View Profile
How to make my program wait for establishing the connection?
« Reply #3 on: December 07, 2011, 08:49:27 pm »
Most networking functions are blocking, it should return only when there's something wrote/read, or an error occured.

Something must be wrong with your code logic, it works flawlessly for me ;)

I suggest debugging and double check if a fonction or variable isn't set twice somewhere. It looks like its server-side to me but I can be wrong

Derp

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to make my program wait for establishing the connection?
« Reply #4 on: December 13, 2011, 01:16:25 pm »
My socket is set to non-blocking.
Client.SetBlocking(false);
When I set it to true, the client hangs. How do I correctly receive packets?
I have HandlePackets() in my main loop:
Code: [Select]
void HandlePackets(){
    while(Client.Receive(packet)==Socket::Done){
            unsigned short PacketID;
            packet >> PacketID;
            switch(PacketID){
                case 0:
                    Func0();
                    break;
                case 1:
                    Func1();
                    break;
                case 2:
                    Func2();
                    break;
                case 3:
                    Func3();
                    break;
                default:
                     break;
            }
        }
}

Derp

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to make my program wait for establishing the connection?
« Reply #5 on: December 13, 2011, 02:27:32 pm »
Lol!
Fixed that by
Code: [Select]
   Client.SetBlocking(true);
    if(Client.Connect(7777, host, 5.f)!=sf::Socket::Done)
        std::cout<<"Connection Error"<<std::endl;
    Client.SetBlocking(false);

and now everything is OK!

nitrix

  • Newbie
  • *
  • Posts: 27
    • View Profile
How to make my program wait for establishing the connection?
« Reply #6 on: December 13, 2011, 08:35:43 pm »
Yep you got it !

I think a socket is blocking by default so you should even be able to get rid of your first line ;3