Добавить
Уведомления

Chat Room

code of server from _thread import * import threading from socket import * s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.bind((host,port)) s.listen(5) clients=[] def connect_new_user(c,ad): while True: m=c.recv(2048) m=ad[0]+':'+m.decode('UTF-8') SendToAll(m,c) def SendToAll (message,connect): for client in clients: if client !=connect : client.send(message.encode('UTF-8')) while True: c,ad = s.accept() clients.append(c) start_new_thread(connect_new_user,(c,ad)) --------------------------------------------------------- #code of clien1 from _thread import * import threading from socket import * def receive_thread(s): while True: x=s.recv(500) print(x.decode('UTF-8')) s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.connect((host,port)) receive=threading.Thread(target=receive_thread,args=(s,) #s socket receive.start() while True: s.send(input("client:").encode('utf-8')) ------------------------------------------ #code of client2 from _thread import * import threading from socket import * def receive_thread(s): while True: x=s.recv(500) print(x.decode('UTF-8')) s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.connect((host,port)) receive=threading.Thread(target=receive_thread,args=(s,) #s socket receive.start() while True: s.send(input("client:").encode('utf-8'))

12+
16 просмотров
2 года назад
12+
16 просмотров
2 года назад

code of server from _thread import * import threading from socket import * s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.bind((host,port)) s.listen(5) clients=[] def connect_new_user(c,ad): while True: m=c.recv(2048) m=ad[0]+':'+m.decode('UTF-8') SendToAll(m,c) def SendToAll (message,connect): for client in clients: if client !=connect : client.send(message.encode('UTF-8')) while True: c,ad = s.accept() clients.append(c) start_new_thread(connect_new_user,(c,ad)) --------------------------------------------------------- #code of clien1 from _thread import * import threading from socket import * def receive_thread(s): while True: x=s.recv(500) print(x.decode('UTF-8')) s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.connect((host,port)) receive=threading.Thread(target=receive_thread,args=(s,) #s socket receive.start() while True: s.send(input("client:").encode('utf-8')) ------------------------------------------ #code of client2 from _thread import * import threading from socket import * def receive_thread(s): while True: x=s.recv(500) print(x.decode('UTF-8')) s=socket(AF_INET,SOCK_STREAM) host='127.0.0.1' port=4000 s.connect((host,port)) receive=threading.Thread(target=receive_thread,args=(s,) #s socket receive.start() while True: s.send(input("client:").encode('utf-8'))

, чтобы оставлять комментарии