聊天应用程序无法在其中使用线程

时间:2019-04-09 15:54:37

标签: python-3.x

我正在尝试制作一个聊天应用,但在使用线程时遇到了麻烦

我尝试了一些调整,但是没有用,还尝试了处理

import socket
import threading

host = '127.0.0.1'
port = 9999
BUFFRATE = 1024
lst_of_clients = []
data = bytes()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

#accepting connection from the clients
def accept_connection(list) :
    while True :
        s.listen()
        conn, addr = s.accept()
        list.append(conn)
        print("connect to :", lst_of_clients)

#recving data from different clients
def recving_data(clients) :
    for x in clients :
        data = recv(BUFFRATE)
        sending_data(data, x)

#sending data to multiple clients
def sending_data(msg, connection) :
    for y in connection:
        y.sendall(data)


t1 = threading.Thread(target = accept_connection, args=  (lst_of_clients,)) 
t2 = threading.Thread(target = recving_data, args = (lst_of_clients,))

#starting the threading
t1.start()
t2.start()

程序未将任何消息发送回客户端

0 个答案:

没有答案