用python完成应用程序,实现两个人的聊天用TCP服务器客户端聊天,要求带输入框和输出框
时间: 2024-02-22 11:58:35 浏览: 146
TCP网络编程2
好的,这里使用 Tkinter 库来实现 GUI 界面。
下面是服务器端代码:
```python
import socket
import threading
import tkinter as tk
class Server:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((self.ip, self.port))
self.server_socket.listen(5)
self.client_socket = None
self.client_addr = None
self.root = tk.Tk()
self.root.title('Server')
self.text_recv = tk.Text(self.root, width=50, height=20)
self.text_recv.pack()
self.text_send = tk.Text(self.root, width=50, height=5)
self.text_send.pack()
self.button_send = tk.Button(self.root, text='Send', command=self.send_message)
self.button_send.pack()
def accept_connection(self):
self.client_socket, self.client_addr = self.server_socket.accept()
self.text_recv.insert('end', 'Client %s:%s connected!\n' % self.client_addr)
def receive_message(self):
while True:
recv_data = self.client_socket.recv(1024)
if not recv_data:
self.text_recv.insert('end', 'Client %s:%s disconnected!\n' % self.client_addr)
break
self.text_recv.insert('end', 'Received message from client: %s\n' % recv_data.decode('utf-8'))
def send_message(self):
send_data = self.text_send.get('1.0', 'end')
self.client_socket.send(send_data.encode('utf-8'))
self.text_send.delete('1.0', 'end')
def start(self):
threading.Thread(target=self.accept_connection).start()
threading.Thread(target=self.receive_message).start()
self.root.mainloop()
if __name__ == '__main__':
server = Server('127.0.0.1', 8000)
server.start()
```
下面是客户端代码:
```python
import socket
import threading
import tkinter as tk
class Client:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.root = tk.Tk()
self.root.title('Client')
self.text_recv = tk.Text(self.root, width=50, height=20)
self.text_recv.pack()
self.text_send = tk.Text(self.root, width=50, height=5)
self.text_send.pack()
self.button_send = tk.Button(self.root, text='Send', command=self.send_message)
self.button_send.pack()
def connect_to_server(self):
try:
self.client_socket.connect((self.ip, self.port))
self.text_recv.insert('end', 'Connected to server %s:%s!\n' % (self.ip, self.port))
except Exception as e:
self.text_recv.insert('end', 'Failed to connect to server %s:%s!\n' % (self.ip, self.port))
self.text_recv.insert('end', str(e) + '\n')
return False
return True
def receive_message(self):
while True:
recv_data = self.client_socket.recv(1024)
if not recv_data:
self.text_recv.insert('end', 'Server %s:%s disconnected!\n' % (self.ip, self.port))
break
self.text_recv.insert('end', 'Received message from server: %s\n' % recv_data.decode('utf-8'))
def send_message(self):
send_data = self.text_send.get('1.0', 'end')
self.client_socket.send(send_data.encode('utf-8'))
self.text_send.delete('1.0', 'end')
def start(self):
if not self.connect_to_server():
return
threading.Thread(target=self.receive_message).start()
self.root.mainloop()
if __name__ == '__main__':
client = Client('127.0.0.1', 8000)
client.start()
```
运行服务器端程序,然后运行两个客户端程序,就可以在不同的客户端之间进行聊天了,每个客户端都有一个输入框和一个输出框。
阅读全文