-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
118 lines (87 loc) · 3.54 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# import required modules
import socket
import threading
import tkinter as tk
from tkinter import scrolledtext
from tkinter import messagebox
HOST = '127.0.0.1'
PORT = 1234
DARK_GREY = '#121212'
MEDIUM_GREY = '#1F1B24'
OCEAN_BLUE = '#464EB8'
WHITE = "white"
FONT = ("Helvetica", 17)
BUTTON_FONT = ("Helvetica", 15)
SMALL_FONT = ("Helvetica", 13)
# Creating a socket object
# AF_INET: we are going to use IPv4 addresses
# SOCK_STREAM: we are using TCP packets for communication
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def add_message(message):
message_box.config(state=tk.NORMAL)
message_box.insert(tk.END, message + '\n')
message_box.config(state=tk.DISABLED)
def connect():
# try except block
try:
# Connect to the server
client.connect((HOST, PORT))
print("Successfully connected to server")
add_message("[SERVER] Successfully connected to the server")
except:
messagebox.showerror("Unable to connect to server", f"Unable to connect to server {HOST} {PORT}")
username = username_textbox.get()
if username != '':
client.sendall(username.encode())
else:
messagebox.showerror("Invalid username", "Username cannot be empty")
threading.Thread(target=listen_for_messages_from_server, args=(client, )).start()
username_textbox.config(state=tk.DISABLED)
username_button.config(state=tk.DISABLED)
def send_message():
message = message_textbox.get()
if message != '':
client.sendall(message.encode())
message_textbox.delete(0, len(message))
else:
messagebox.showerror("Empty message", "Message cannot be empty")
root = tk.Tk()
root.geometry("600x600")
root.title("Messenger Client")
root.resizable(False, False)
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=4)
root.grid_rowconfigure(2, weight=1)
top_frame = tk.Frame(root, width=600, height=100, bg=DARK_GREY)
top_frame.grid(row=0, column=0, sticky=tk.NSEW)
middle_frame = tk.Frame(root, width=600, height=400, bg=MEDIUM_GREY)
middle_frame.grid(row=1, column=0, sticky=tk.NSEW)
bottom_frame = tk.Frame(root, width=600, height=100, bg=DARK_GREY)
bottom_frame.grid(row=2, column=0, sticky=tk.NSEW)
username_label = tk.Label(top_frame, text="Enter username:", font=FONT, bg=DARK_GREY, fg=WHITE)
username_label.pack(side=tk.LEFT, padx=10)
username_textbox = tk.Entry(top_frame, font=FONT, bg=MEDIUM_GREY, fg=WHITE, width=23)
username_textbox.pack(side=tk.LEFT)
username_button = tk.Button(top_frame, text="Join", font=BUTTON_FONT, bg=OCEAN_BLUE, fg=WHITE, command=connect)
username_button.pack(side=tk.LEFT, padx=15)
message_textbox = tk.Entry(bottom_frame, font=FONT, bg=MEDIUM_GREY, fg=WHITE, width=38)
message_textbox.pack(side=tk.LEFT, padx=10)
message_button = tk.Button(bottom_frame, text="Send", font=BUTTON_FONT, bg=OCEAN_BLUE, fg=WHITE, command=send_message)
message_button.pack(side=tk.LEFT, padx=10)
message_box = scrolledtext.ScrolledText(middle_frame, font=SMALL_FONT, bg=MEDIUM_GREY, fg=WHITE, width=67, height=26.5)
message_box.config(state=tk.DISABLED)
message_box.pack(side=tk.TOP)
def listen_for_messages_from_server(client):
while 1:
message = client.recv(2048).decode('utf-8')
if message != '':
username = message.split("~")[0]
content = message.split('~')[1]
add_message(f"[{username}] {content}")
else:
messagebox.showerror("Error", "Message recevied from client is empty")
# main function
def main():
root.mainloop()
if __name__ == '__main__':
main()