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

Author Topic: Best way to map clients with a Selector.  (Read 4799 times)

0 Members and 1 Guest are viewing this topic.

Manux

  • Newbie
  • *
  • Posts: 23
    • View Profile
Best way to map clients with a Selector.
« on: December 30, 2009, 05:33:43 am »
Just wondering before I get into it, what's the best way to map sf::SocketTCPs returned by Selector.GetSocketReady(i) to be able to retreive the correct client.
My first guess is that making a std::map<sf::Socket,WhateverClientClass*> wouldn't work because the selector method doesn't seem to return a reference (nor a pointer).
My second guess is that the obvious solution seems to be making a vector, and searching it(since the == operator is overloaded).

Does the first solution work? Or is there a more efficient one than the second?
(In a situation where lots of clients would send lots of small packets)

Oh and by the way... I can only seem to bind to "localhost", I would say that's not normal, but then the doc doesnt talk much about it... or am I blind?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Best way to map clients with a Selector.
« Reply #1 on: December 30, 2009, 10:56:26 am »
The first solution works, sf::Socket defines an operator < that compares the low-level socket handle (thus it doesn't matter it you compare copies of the same socket).

Quote
Oh and by the way... I can only seem to bind to "localhost", I would say that's not normal, but then the doc doesnt talk much about it... or am I blind?

Do you mean "connect" to localhost?
Laurent Gomila - SFML developer

Manux

  • Newbie
  • *
  • Posts: 23
    • View Profile
Best way to map clients with a Selector.
« Reply #2 on: December 30, 2009, 04:43:30 pm »
Hmm when a server awaits for incoming connections, it listens on a port. But in other net libraries I used one would always specifiy an IP(the local IP usually), or "localhost" (127.0.0.1) along with a port to bind on.

I have not found where we specify such a thing with SFML...

In Python one would do this:
Code: [Select]
import socket

HOST = socket.gethostbyname(socket.gethostname()) #that should return 192.168.1.1xx          
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()


edit: oh well... for some unknown reason, restarting my computer made it possible for me to connect to a sf::SocketTCP other than from localhost

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Best way to map clients with a Selector.
« Reply #3 on: December 30, 2009, 06:02:46 pm »
The Bind function is for UDP sockets, it has no effect here with TCP.
Laurent Gomila - SFML developer

Shimajda

  • Newbie
  • *
  • Posts: 17
    • View Profile
Best way to map clients with a Selector.
« Reply #4 on: June 16, 2010, 06:41:18 pm »
Hi,
I cant get std::map to work with selector

Heres server code:
http://www.copypastecode.com/31020/
Client code:
http://www.copypastecode.com/31024/
(for tests just std::cin before disconnect to prevent client from disconnection and packet sending can be deleted)

Now my problem:
Lets say we connect 100 clients TC (connMap.size()) will be 100 after closing all clients TC will be 37 and it should be 0. Whats going on?

It looks like the selector dont execute the disconnection part for sockets connected after 63.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Best way to map clients with a Selector.
« Reply #5 on: June 16, 2010, 07:13:07 pm »
Actually, the number of sockets that a selector can hold is OS-dependent. And it may perfectly be as low as 64.
Laurent Gomila - SFML developer

Shimajda

  • Newbie
  • *
  • Posts: 17
    • View Profile
Best way to map clients with a Selector.
« Reply #6 on: June 16, 2010, 07:18:17 pm »
Thanks :)
Wasted half a day to do the impossible, I should ask earlier.  :oops:

Edit:
How can I check count of socket that selector contains?
Edit2:
How can I check if selector contains given socket?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Best way to map clients with a Selector.
« Reply #7 on: June 16, 2010, 07:51:17 pm »
Quote
How can I check count of socket that selector contains?

You can't. sf::Selector is not a container, you should store this kind of information outside if you need it.

Quote
How can I check if selector contains given socket?

You can't. Same answer as above :)
Laurent Gomila - SFML developer

Shimajda

  • Newbie
  • *
  • Posts: 17
    • View Profile
Best way to map clients with a Selector.
« Reply #8 on: June 16, 2010, 07:53:20 pm »
Thanks just asked to be sure.