如何向连接到我的服务器(套接字)的所有客户端发送消息

发布于 2025-01-12 14:27:24 字数 1565 浏览 0 评论 0原文

我的代码如下,我似乎无法弄清楚如何立即向我的客户发送消息

即使当我尝试寻求帮助或类似的事情时,他们也会说存储每个客户并不知道如何做到这一点。我也尝试过并查找它我做到了,但我希望它像不和谐一样立即更新

import socket
import threading

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!stop"
NAME_MESSAGE = "!name"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(client, addr):
    print(f'[INFO] {addr} connected.')
    while True:
        msg_length = client.recv(HEADER).decode(FORMAT)
        if not msg_length:
            msg_length = int(msg_length)
            msg = client.recv(msg_length).decode(FORMAT)

            if msg == DISCONNECT_MESSAGE:
                print(f'[DISCONNECT] {addr} Disconnected')
                break

            elif msg == NAME_MESSAGE:
                name_length = client.recv(HEADER).decode(FORMAT)
                if name_length:
                    name_length = int(name_length)
                    name = client.recv(name_length).decode(FORMAT)
                    addr = name
            else:
                <Idk what to put here>
        else:
            break
            

def start():
    server.listen()
    print(f'[INFO] Server is listening on {SERVER}')
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f'\n[INFO] Active connections: {threading.activeCount() - 1}')

print('[INFO] Server is starting...')
start()

My code is below and I just can't seem to figure out how to send messages to my clients at once

Even when I try asking for help or anything like that they say Store each client and idk how to do that. also I did try it and look it up I did it but I want it to update instantly like discord

import socket
import threading

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!stop"
NAME_MESSAGE = "!name"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(client, addr):
    print(f'[INFO] {addr} connected.')
    while True:
        msg_length = client.recv(HEADER).decode(FORMAT)
        if not msg_length:
            msg_length = int(msg_length)
            msg = client.recv(msg_length).decode(FORMAT)

            if msg == DISCONNECT_MESSAGE:
                print(f'[DISCONNECT] {addr} Disconnected')
                break

            elif msg == NAME_MESSAGE:
                name_length = client.recv(HEADER).decode(FORMAT)
                if name_length:
                    name_length = int(name_length)
                    name = client.recv(name_length).decode(FORMAT)
                    addr = name
            else:
                <Idk what to put here>
        else:
            break
            

def start():
    server.listen()
    print(f'[INFO] Server is listening on {SERVER}')
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f'\n[INFO] Active connections: {threading.activeCount() - 1}')

print('[INFO] Server is starting...')
start()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文