import socket
import threading
import json
import tkinter as tk
from tkinter import scrolledtext
import hashlib
# 客户端UI
class IMClient:
def __init__(self, root):
self.root = root
self.root.title('IM Client')
self.username = tk.StringVar()
self.password = tk.StringVar()
self.receiver = tk.StringVar()
self.message = tk.StringVar()
# UI组件
self.label_username = tk.Label(root, text='Username:')
self.label_username.grid(row=0, column=0, padx=10, pady=10)
self.entry_username = tk.Entry(root, textvariable=self.username)
self.entry_username.grid(row=0, column=1, padx=10, pady=10)
self.label_password = tk.Label(root, text='Password:')
self.label_password.grid(row=1, column=0, padx=10, pady=10)
self.entry_password = tk.Entry(root, show='*', textvariable=self.password)
self.entry_password.grid(row=1, column=1, padx=10, pady=10)
self.login_button = tk.Button(root, text='Login', command=self.login)
self.login_button.grid(row=2, column=0, columnspan=2, pady=20)
self.chat_window = scrolledtext.ScrolledText(root, width=50, height=20)
self.chat_window.grid(row=3, column=0, columnspan=2, padx=10, pady=10)
self.label_receiver = tk.Label(root, text='Receiver:')
self.label_receiver.grid(row=4, column=0, padx=10, pady=10)
self.entry_receiver = tk.Entry(root, textvariable=self.receiver)
self.entry_receiver.grid(row=4, column=1, padx=10, pady=10)
self.label_message = tk.Label(root, text='Message:')
self.label_message.grid(row=5, column=0, padx=10, pady=10)
self.entry_message = tk.Entry(root, textvariable=self.message)
self.entry_message.grid(row=5, column=1, padx=10, pady=10)
self.send_button = tk.Button(root, text='Send', command=self.send_message)
self.send_button.grid(row=6, column=0, columnspan=2, pady=20)
# 初始化socket连接
self.server_ip = '127.0.0.1' # 服务器IP地址
self.server_port = 12345 # 服务器端口号
self.client_socket = None
# 启动接收消息线程
self.receive_thread = threading.Thread(target=self.receive_messages)
self.receive_thread.daemon = True
self.receive_thread.start()
def login(self):
# 在这里添加登录逻辑(例如,验证用户名和密码)
# 由于这个示例代码仅用于演示,我们直接连接服务器
try:
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((self.server_ip, self.server_port))
self.chat_window.insert(tk.END, "Connected to server\n")
except Exception as e:
self.chat_window.insert(tk.END, f"Failed to connect to server: {e}\n")
def send_message(self):
if self.client_socket and self.receiver.get() and self.message.get():
message_data = {
'type': 'message',
'sender': self.username.get(),
'receiver': self.receiver.get(),
'content': self.message.get()
}
self.client_socket.sendall(json.dumps(message_data).encode('utf-8'))
self.chat_window.insert(tk.END, f"You: {self.message.get()}\n")
self.message.set('') # 清空消息输入框
def receive_messages(self):
while self.client_socket:
try:
data = self.client_socket.recv(1024).decode('utf-8')
if data:
message = json.loads(data)
if message['type'] == 'message':
self.chat_window.insert(tk.END, f"{message['sender']}: {message['content']}\n")
except Exception as e:
self.chat_window.insert(tk.END, f"Error receiving message: {e}\n")
break
if __name__ == "__main__":
root = tk.Tk()
client = IMClient(root)
root.mainloop()
|