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

Author Topic: Multiple connection using TCP  (Read 2304 times)

0 Members and 1 Guest are viewing this topic.

WillYoung

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://squaredworld.co.uk
    • Email
Multiple connection using TCP
« on: March 19, 2011, 05:38:55 pm »
Hello everyone!
Been programming with SFML for about a month now, but this is my first problem (which is most likely to be trivial).

I am not using a selector as I want to keep to a structure which has been previously planned, I have turned to sockets into nonblocking and every 'game loop' it checks for a new connection:
Code: [Select]

 sf::IPAddress ConnectionAddress;
    sf::SocketTCP Connection;
    if (ServerSocket.Accept(Connection,&ConnectionAddress) == sf::Socket::Done)
    {


I have a:
Code: [Select]
sf::SocketTCP ServerSocket

Which is effectively the listener so when a new connection is 'Done' see above.

To store the client connections I have a vector which contains this:
Code: [Select]

sf::IPAddress Address;
sf::SocketTCP Connection;


Which is inherited from the new connection function. (It also contains various other data connected to the client but that is not relevant).

My problem is when the server gets a new connection it works fine and appears to be 'Done' and the client is processed into the vector. But when a second Client connects it doesn't notice it, until the first disconnects.

What might be holding me back is the theory of how SFML works as this structure should work in my eyes? What am I misinterpreting.

Thanks in advance!
Will,

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple connection using TCP
« Reply #1 on: March 19, 2011, 06:08:30 pm »
That should work, so I think there's something wrong in your code that we don't see. Could you write a minimal and complete example that reproduces the problem? That would be much easier to debug.
Laurent Gomila - SFML developer

WillYoung

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://squaredworld.co.uk
    • Email
Multiple connection using TCP
« Reply #2 on: March 19, 2011, 06:19:35 pm »
Hello!
Firstly well done on this amazing library, secondly you are very much correct. I boiled it down to a minimalistic program and I suddenly realized while doing so that I had not set:
Code: [Select]

ServerSocket.SetBlocking(0);//Make sure it doesn't hang.

To the new connection as I thought you only had to do it to the listener, I think I have already proved my self as a 'HerpDerp' but I couldn't find anything about it in the documentation so I was blindly stabbing into the dark, Hehe.

If anyone reads this thread then the problem was that you need to SetBlocking(0); To all sockets, not just the server!  Unlike SDL.

Regards,
Will.