0

This question is similar to this one, but that was for JavaScript whereas mine is for Python.

How do I send a message to every connected client from the server except a selected client in Python using the sockets library?

I am making a simple game, where I want to detect the first person to press a button among three clients, and then notify the other two clients that they lost while notifying the winner that they won.

Usually, to send information to a client you do (on a new thread):

connected_client.sendall(data)

To receive, you do:

data = socket.recv()

But from what I searched, I couldn't find a way to send data to every connected client except a certain client.

I thought I could get around this by creating an 'identifying name' for each thread which ran the receiving function, but I couldn't find a good way to do this due to which I decided to search for a better option.

How can I do this?

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
leaves
  • 76
  • 1
  • 10
  • If you're sending a broadcast message, you can attach an `ignore` tag with the identifiers of clients that should ignore the message. If a client recognise itself, then it will drop the message. – crissal Jun 28 '21 at 14:04
  • @crissal By identifiers of clients do you mean identifiers of the thread in which the receiving function is in or can you obtain a different identifier of a client? – leaves Jun 28 '21 at 14:06
  • Anything that is unique will work as identifier; btw your sender should know the various identifiers, and send to one rather than another accordingly. – crissal Jun 28 '21 at 14:11
  • What does: `first person to press a button` mean when you are talking about socket connections? – quamrana Jun 28 '21 at 14:14
  • @quamrana Its the concept of the game I'm making, just to give you an idea of why I need an answer to this – leaves Jun 28 '21 at 14:15
  • Are you saying that: `press a button` gets translated to `receive a message` about pressing a button? – quamrana Jun 28 '21 at 14:17
  • @crissal How do I set an identifier? (I'm new to the sockets library) – leaves Jun 28 '21 at 14:17
  • @quamrana Yes, the button press sends a message to the server. – leaves Jun 28 '21 at 14:19
  • If they're thread, you can read about [`get_ident()`](https://docs.python.org/3/library/threading.html#threading.get_ident), or you can set your own unique `name`. – crissal Jun 28 '21 at 14:19
  • So, when you receive a message, you know who sent it. So now you know who to congratulate and by default the others you commiserate with. – quamrana Jun 28 '21 at 14:20

0 Answers0