Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Script for Tkinter GUI chat client."""
- from socket import AF_INET, socket, SOCK_STREAM
- from threading import Thread
- import tkinter
- def receive():
- """Handles receiving of messages."""
- while True:
- try:
- msg = client_socket.recv(BUFSIZ).decode("utf8")
- msg_list.insert(tkinter.END, msg)
- except OSError: # Possibly client has left the chat.
- break
- def send(event=None): # event is passed by binders.
- """Handles sending of messages."""
- msg = my_msg.get()
- my_msg.set("") # Clears input field.
- client_socket.send(bytes(msg, "utf8"))
- if msg == "{quit}":
- client_socket.close()
- top.quit()
- def on_closing(event=None):
- """This function is to be called when the window is closed."""
- my_msg.set("{quit}")
- send()
- top = tkinter.Tk()
- top.title("CS201")
- messages_frame = tkinter.Frame(top)
- my_msg = tkinter.StringVar() # For the messages to be sent.
- my_msg.set("Name")
- scrollbar = tkinter.Scrollbar(messages_frame) # To navigate through past messages.
- msg_list = tkinter.Listbox(messages_frame, height=15, width=50, yscrollcommand=scrollbar.set)
- scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
- msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
- msg_list.pack()
- messages_frame.pack()
- entry_field = tkinter.Entry(top, textvariable=my_msg)
- entry_field.bind("<Return>", send)
- entry_field.pack()
- send_button = tkinter.Button(top, text="Send", command=send)
- send_button.pack()
- top.protocol("WM_DELETE_WINDOW", on_closing)
- HOST = input('Enter host: ')
- PORT = input('Enter port: ')
- if not PORT:
- PORT = 33000 # Default value.
- else:
- PORT = int(PORT)
- BUFSIZ = 1024
- ADDR = (HOST, PORT)
- client_socket = socket(AF_INET, SOCK_STREAM)
- client_socket.connect(ADDR)
- receive_thread = Thread(target=receive)
- receive_thread.start()
- tkinter.mainloop() # Starts GUI execution.
- =============================== SERVER
- #!/usr/local/bin/python3.6
- """Server for multithreaded (asynchronous) chat application."""
- from socket import AF_INET, socket, SOCK_STREAM
- from threading import Thread
- def accept_incoming_connections():
- """Sets up handling for incoming clients."""
- while True:
- client, client_address = SERVER.accept()
- print("%s:%s has connected." % client_address)
- client.send(bytes("Greetings from the cave! Now type your name and press enter!", "utf8"))
- addresses[client] = client_address
- Thread(target=handle_client, args=(client,)).start()
- def handle_client(client): # Takes client socket as argument.
- """Handles a single client connection."""
- name = client.recv(BUFSIZ).decode("utf8")
- welcome = 'Welcome %s! If you ever want to quit, type {quit} to exit.' % name
- client.send(bytes(welcome, "utf8"))
- msg = "%s has joined the chat!" % name
- broadcast(bytes(msg, "utf8"))
- clients[client] = name
- while True:
- msg = client.recv(BUFSIZ)
- if msg != bytes("{quit}", "utf8"):
- broadcast(msg, name+": ")
- else:
- client.send(bytes("{quit}", "utf8"))
- client.close()
- del clients[client]
- broadcast(bytes("%s has left the chat." % name, "utf8"))
- break
- def broadcast(msg, prefix=""): # prefix is for name identification.
- """Broadcasts a message to all the clients."""
- for sock in clients:
- sock.send(bytes(prefix, "utf8")+msg)
- clients = {}
- addresses = {}
- HOST = ''
- PORT = 33000
- BUFSIZ = 1024
- ADDR = (HOST, PORT)
- SERVER = socket(AF_INET, SOCK_STREAM)
- SERVER.bind(ADDR)
- if __name__ == "__main__":
- SERVER.listen(5)
- print("Waiting for connection...")
- ACCEPT_THREAD = Thread(target=accept_incoming_connections)
- ACCEPT_THREAD.start()
- ACCEPT_THREAD.join()
- SERVER.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement