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
  • 18,379
  • 16
  • 47
  • 61
leaves
  • 111
  • 1
  • 12
  • 1
    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

1 Answers1

0

Inserting them into a list can help. For example...

For the server side:

import socket
import threading

# This is where you store all of your Client IP's
client_list = []

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "yourip"
server_port = 8888
server.bind((server_ip, server_port))

def check_client(client_ip):
    while True:
        data = client_ip.recv(1024).decode()
        
        if "condition" in data:
            for ip in client_list:
                if ip != client_ip:
                    ip.send("something".encode())

def check_connection():
    server.listen()
    while True:
        client_ip, client_address = server.accept()
        client_list.append(client_ip)
        threading.Thread(target=check_client, args=(client_ip,), daemon=True).start()

check_connection()

So what happens is you call the check_connection function to check for incoming connections. After it receives one, it appends the connection inside the client_list variable. At the same time, it creates a thread to the current connection, check_client, which checks for any info being sent. If there's an info being sent by one of your clients, it checks if the "condition" string is inside your sent data. If so, it sends "something" string into all of your clients with exception to itself. Take note that when you send data, it must be in bytes.

For the client side:

import socket
import threading

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "serverip"
server_port = 8888
server.connect((server_ip, server_port))


def receive_info():
    while True:
        data = server.recv(1024).decode()

        if "something" in data:
            print("Someone already sent something")


threading.Thread(target=receive_info, daemon=True).start()

while True:
    user_input = input("Type 'condition': ")
    server.send(user_input.encode())

What this only does is, it sends your input into the server. If you typed "condition" on your input, it will send "something" on the other clients except you. So you need to setup 2 more clients in order to see the results.

Don't forget to set server_ip and server_port's values!

YaNawMe
  • 16
  • 1