-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
87 lines (66 loc) · 2.46 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
import socket
import threading
import tkinter as tk
from tkinter import scrolledtext, Entry, Button
from DES import DES
KEY = 'secret_k'
def receive():
while True:
global KEY
try:
# Receive data from the server
data = client_socket.recv(1024)
if not data:
break
d = DES(KEY)
# Update the UI with the received message
client_display.insert(tk.END, f"Server: {d.decrypt(data.decode('utf-8'))}\n")
client_display.yview(tk.END)
except:
break
def send_message():
global KEY
# Get the message from the entry widget
message = client_entry.get()
d = DES(KEY)
# Send the message to the server
client_socket.send(d.encrypt(message).encode('utf-8'))
# Update the client UI with the sent message
client_display.insert(tk.END, f"Client: {message}\n")
client_display.yview(tk.END)
# Clear the entry widget after sending the message
client_entry.delete(0, tk.END)
# Function to connect to the server with the provided IP address
def connect_to_server():
global client_socket
server_ip = server_entry.get()
try:
# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_ip, 5555))
# Create a thread to receive messages from the server
receive_thread = threading.Thread(target=receive)
receive_thread.start()
# Enable the message entry and send button
client_entry.config(state=tk.NORMAL)
send_button.config(state=tk.NORMAL)
# Disable the connect button
connect_button.config(state=tk.DISABLED)
except Exception as e:
client_display.insert(tk.END, f"Error connecting to the server: {str(e)}\n")
client_display.yview(tk.END)
# Set up the client UI
client = tk.Tk()
client.title("Chat Client")
client_display = scrolledtext.ScrolledText(client, width=40, height=10)
client_display.pack(padx=10, pady=10)
server_entry = Entry(client, width=30)
server_entry.pack(padx=10, pady=10)
connect_button = Button(client, text="Connect", command=connect_to_server)
connect_button.pack(padx=10, pady=10)
client_entry = Entry(client, width=30, state=tk.DISABLED)
client_entry.pack(padx=10, pady=10)
send_button = Button(client, text="Send", command=send_message, state=tk.DISABLED)
send_button.pack(padx=10, pady=10)
# Start the Tkinter main loop
client.mainloop()