一、界面功能展示
1、设置一个通信 用户1
2、设置通信 用户2
3、进入聊天功能界面
4、发送信息来实现实时通信
二、代码实现
1、服务器端
(服务器需要能够与客户机进行直接通信,客户机之间不需要能够通信)
服务器需要配置监听的IP = '0.0.0.0' 表示所有 port= '端口随意但是要与客户端配置的端口相同'
import socket
import threading
def handle_client(client_socket, clients):
while True:
try:
data = client_socket.recv(1024)
if not data:
break
for client in clients:
if client != client_socket:
client.send(data)
except:
break
client_socket.close()
def main():
host = '0.0.0.0'
port = 1996
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(5)
print(f"[*] Listening on {host}:{port}")
clients = []
while True:
client_socket, addr = server.accept()
print(f"[*] Accepted connection from {addr[0]}:{addr[1]}")
clients.append(client_socket)
client_handler = threading.Thread(target=handle_client, args=(client_socket, clients))
client_handler.start()
if __name__ == "__main__":
main()
2、客户端
①localhost是在本地调试使用,如果服务器是其他IP,需要进行配置,
②端口与服务器需要对应
import socket
import threading
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextBrowser, QLineEdit, QVBoxLayout, QWidget, QPushButton, QInputDialog, QDesktopWidget
from PyQt5.QtCore import Qt
class ChatClient(QMainWindow):
def __init__(self):
super().__init__()
self.username = None # 初始化用户名为None
self.init_ui()
def init_ui(self):
self.chat_window = QTextBrowser()
self.input_field = QLineEdit()
self.send_button = QPushButton("Send")
self.send_button.clicked.connect(self.send_message)
self.exit_button = QPushButton("Exit")
self.exit_button.clicked.connect(self.exit)
layout = QVBoxLayout()
layout.addWidget(self.chat_window)
layout.addWidget(self.input_field)
layout.addWidget(self.send_button)
layout.addWidget(self.exit_button)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.setWindowTitle("Chat Client")
self.setGeometry(0, 0, 400, 400) # 初始位置和大小设置为0, 0, 400, 400
self.center_window() # 调用居中方法
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect(("localhost", 9999))
self.receive_thread = threading.Thread(target=self.receive_messages)
self.receive_thread.start()
# 监听回车键事件
self.input_field.returnPressed.connect(self.send_message)
# 弹出对话框设置用户名
self.set_username()
def center_window(self):
# 获取屏幕尺寸
screen_geometry = QDesktopWidget().screenGeometry()
window_geometry = self.geometry()
# 计算居中位置
x = (screen_geometry.width() - window_geometry.width()) // 2
y = (screen_geometry.height() - window_geometry.height()) // 2
self.move(x, y) # 将窗口移动到居中位置
def set_username(self):
self.username, ok = self.get_text_input("Set Username", "Enter your username:")
if not ok:
self.close()
else:
self.setWindowTitle(f"Chat Client - {self.username}")
def get_text_input(self, title, prompt):
text, ok = QInputDialog.getText(self, title, prompt)
return (text.strip(), ok)
def receive_messages(self):
while True:
try:
message = self.client_socket.recv(1024).decode("utf-8")
if message == "exit":
break
self.chat_window.append(message)
except:
break
def send_message(self):
message = self.input_field.text()
if message:
full_message = f"{self.username}: {message}"
self.client_socket.send(full_message.encode("utf-8"))
self.input_field.clear()
def exit(self):
self.client_socket.send("exit".encode("utf-8"))
self.client_socket.close()
sys.exit()
if __name__ == "__main__":
app = QApplication(sys.argv)
client = ChatClient()
client.show()
sys.exit(app.exec_())
文章评论