73 lines
1.5 KiB
Python
73 lines
1.5 KiB
Python
from tkinter import *
|
|
import socket
|
|
import threading
|
|
window = Tk()
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
# def allconn():
|
|
# global all_conn
|
|
# all_conn = []
|
|
#
|
|
# allconn()
|
|
|
|
all_conn = []
|
|
|
|
window.geometry('1200x600')
|
|
host = socket.gethostname()
|
|
|
|
ip = '192.168.1.6'
|
|
s.bind((ip,9000))
|
|
|
|
chattext = Text(width = 50)
|
|
|
|
chattext.place(x=200, y=200)
|
|
|
|
connecteduserslb = Label(text = 'connected users: ')
|
|
connecteduserslb.place(x=200, y=50)
|
|
|
|
names_dic = {}
|
|
|
|
def recieveandsend():
|
|
while 1:
|
|
all_conn = list(names_dic.keys())
|
|
for client in all_conn:
|
|
client.settimeout(0.1)
|
|
try:
|
|
msg = f'{names_dic[client]}: {client.recv(1024).decode()}'
|
|
except:
|
|
pass
|
|
else:
|
|
for client_ in all_conn:
|
|
try:
|
|
client_.send(msg.encode())
|
|
chattext.insert(INSERT, msg)
|
|
except:
|
|
pass
|
|
|
|
def accept():
|
|
global names_dic
|
|
while True:
|
|
try:
|
|
s.listen(5)
|
|
connection, address = s.accept()
|
|
print('listened')
|
|
except socket.timeout:
|
|
pass
|
|
else:
|
|
names_dic[connection] = connection.recv(1024).decode()
|
|
|
|
|
|
|
|
accept_thread = threading.Thread(target=accept)
|
|
accept_thread.start()
|
|
|
|
recieveandsend_thread = threading.Thread(target=recieveandsend)
|
|
recieveandsend_thread.start()
|
|
|
|
|
|
window.mainloop()
|
|
|
|
|