Python server - How to communicate between multiple clients?

124
November 14, 2021, at 01:10 AM

I'm writing a python3 server using socket and selectors, I want to achieve the communication between clients. But I can only send the message back to client themselves instead of other clients.

I'm confused about how to do this, could anyone help me please? Thanks in advance!

Here's the code I've written so far:

client1:

import socket
import sys
messages = [b'Hi from client1',
            b'Good morning from client1',
            ] 
server_address = ('localhost', 1234) 
socks = [ socket.socket(socket.AF_INET, 
socket.SOCK_STREAM) 
for i in range(1)] 
print('connecting to %s port %s' % server_address) 
for s in socks:
    s.connect(server_address)
for message in messages:
    for s in socks:
        s.send(message)
    for s in socks: 
        data = s.recv(1024)
        print(data.decode())
        if not data: 
            print(sys.stderr, 'closing socket', s.getsockname())

client2:

import socket
import sys
messages = [b'Hi from client2',
            b'Good luck from client2',
            ] 
server_address = ('localhost', 1234) 
socks = [ socket.socket(socket.AF_INET, 
socket.SOCK_STREAM) 
for i in range(1)] 
print('connecting to %s port %s' % server_address) 
for s in socks:
    s.connect(server_address)
for message in messages:
    for s in socks:
        s.send(message)
    for s in socks: 
        data = s.recv(1024)
        print(data.decode())
        if not data: 
            print(sys.stderr, 'closing socket', s.getsockname())

Server:

import selectors
import socket
sel = selectors.DefaultSelector()
all_msg = []
def accept(sock, mask):
    conn, addr = sock.accept()
    conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, read)
def read(conn, mask):
    data = conn.recv(1024)
    if data:
        conn.send(data) # echo data back to client themselves, but how to send to other client??
    else:
        sel.unregister(conn)
        conn.close()
sock = socket.socket()
sock.bind(('localhost', 1234))
sock.listen(1)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)
while True:
    events = sel.select()
    for key, mask in events:
        callback = key.data
        callback(key.fileobj, mask)
READ ALSO
Array of Generic List

Array of Generic List

I am playing with Generic and arrays, it seems the following code compiles fine,

68
Resize Image on phone after orientation change in next.js

Resize Image on phone after orientation change in next.js

After fighting with the Image component from Nextjs, and finally befriending it more and more, I have one final issue I can't seem to figure out

49
How to show validation error in form in react js?

How to show validation error in form in react js?

I have created a form and connected it to server

29
JSON data from one to many relationships in MYSQL using PHP [closed]

JSON data from one to many relationships in MYSQL using PHP [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

56