Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import threading
- from tkinter import *
- from tkinter import scrolledtext
- root = Tk()
- root.geometry("650x390")
- output = scrolledtext.ScrolledText(root, wrap = WORD, bg='black', fg="lime", font = (13))
- output.pack()
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- firstTime = True
- PORT = 8080
- SERVER = '192.168.1.6'
- print(SERVER)
- ADDR = (SERVER, PORT)
- FORMAT = 'utf-8'
- CLIENTS = set()
- CLIENTSA = 0
- server.bind(ADDR)
- def handle_client(conn, addr):
- global CLIENTS
- global CLIENTSA
- global firstTime
- output.insert(INSERT, "\n ACTIVE CLIENTS: %s" %CLIENTSA)
- output.insert(INSERT,f'\n[NEW CONNECTION] {addr} connected')
- connected = True
- while connected:
- try:
- msg = conn.recv(1024)
- output.insert(INSERT," \n[CLIENT] " + msg.decode(FORMAT))
- print("got msg: " + msg.decode())
- for client in CLIENTS:
- print(msg.decode(FORMAT).split(","))
- if "FirstTime" in msg.decode(FORMAT).split(','):
- message = msg.decode(FORMAT).split(',')[1] + " Joined the Cave"
- client.send(message.encode(FORMAT))
- output.insert(INSERT, f"\n{message}")
- else:
- client.send(msg)
- output.insert(INSERT, "\nmessage send")
- except:
- conn.close()
- output.insert(INSERT, "\nCLIENT LEFT")
- CLIENTSA -= 1
- break
- def start():
- global CLIENTSA
- output.insert(INSERT,'SERVER STARTED')
- server.listen()
- while True:
- conn, addr = server.accept()
- thread = threading.Thread(target=handle_client,args=(conn,addr), daemon=True)
- CLIENTS.add(conn)
- CLIENTSA += 1
- thread.start()
- startThread = threading.Thread(target=start, daemon=True)
- startThread.start()
- root.mainloop()
Add Comment
Please, Sign In to add comment