当前位置:   article > 正文

python socket编程8 - PyQt6界面实现TCP server/client 多客户端通讯的例子_pyqt tcp

pyqt tcp

一、小总结:

本篇在单机通讯的基础上进行重构,实现多线程TCP server与多个TCP client通讯的例子。

创建两个 PyQt6的项目,一个作为TCP server 项目,另一个作为TCP client项目。

二、TCP server部分

1、TCPServer 类定义中增加数组模拟线程池

在这里插入图片描述

2、消息发送修改为遍历线程池

在这里插入图片描述

3、停止服务修改为遍历线程池

在这里插入图片描述

4、TCP server完整代码

import socket

from PyQt6.QtCore import QThread, pyqtSignal


class TCPServer:

    def __init__(self, ui, server_ip, server_hostname, server_port):

        self.ui = ui  # 主界面
        self.ip = server_ip  # 服务器ip地址
        self.port = server_port  # 服务器端口号
        self.serverName = server_hostname  # 显示名称
        self.is_running = False  # 是否已经启动

        self.socket = None  # socket
        # self.socketThread = None  # 新的 socket receive 线程
        self.connectedThreadPool = []  # 模拟线程池,替代上面的 socket receive 线程
        self.ui.statusbar.showMessage("服务已经启动,等待客户端的连接......")
        self.start()

    def start(self):
        if not self.is_running:
            self.is_running = True
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.socket.bind((self.ip, self.port))  # 绑定IP与端口
            self.socket.listen(10)  # 设定最大连接数
            self.startSocketReceiveThread()

    def stop(self):
        try:
            if self.is_running:
                self.is_running = False
                for connectThread in self.connectedThreadPool:
                    if connectThread.is_running:
                        connectThread.stop()
        except Exception as e:
            print(e)

    def startSocketReceiveThread(self):
        """
        启动一个新的监听线程,等待连接。
        :return:
        """
        socketThread = TCPServerSocketReceiveThread(self.socket)
        self.connectedThreadPool.append(socketThread)
        socketThread.clientConnection.connect(self.socket_client_connect_trigger)
        socketThread.receivedClientData.connect(self.show_client_message)
        socketThread.serverStatus.connect(self.server_status_trigger)
        socketThread.start()

    def server_status_trigger(self, status):
        self.ui.statusbar.showMessage(status)

    def socket_client_connect_trigger(self, state):
        if state == 'connect':
            self.startSocketReceiveThread()
        else:
            self.ui.statusbar.showMessage("客户端已经断开。")

    def show_client_message(self, message):
        self.ui.textEdit.append(message)

    def send_message_to_client(self, message):  # 改为广播
        if self.is_running:
            message = self.serverName + ':' + message
            self.ui.textEdit.append(message)
            for connectThread in self.connectedThreadPool:
                if connectThread.is_connected:
                    connectThread.send_data_to_client(message)


class TCPServerSocketReceiveThread(QThread):
    clientConnection: pyqtSignal = pyqtSignal(str)  # 向主线程发送连接状态标志
    receivedClientData: pyqtSignal = pyqtSignal(str)  # 向主线程发送接受到客户端的数据
    serverStatus: pyqtSignal = pyqtSignal(str)  # 向主线程发送服务器状态

    def __init__(self, serverSocket):
        super(TCPServerSocketReceiveThread, self).__init__()
        self.serverSocket = serverSocket
        self.clientSocket = None
        self.addr = None
        self.clientIP = None
        self.clientPort = None
        self.is_running = True
        self.is_connected = False

    def run(self):
        self.clientSocket, self.addr = self.serverSocket.accept()  # 接受客户端的连接
        self.is_connected = True
        self.emitConnectEvent('connect')  # 发送客户端连接成功通知到主界面
        self.clientIP, self.clientPort = self.addr
        self.serverStatus.emit("客户端【" + self.clientIP + "】已经连接。")
        self.startReceiveData()

    def startReceiveData(self):

        while self.is_running:
            try:
                data = self.clientSocket.recv(1024).decode('utf-8')  # 接受到字符串并按照utf-8编译
                if not data:
                    self.emitConnectEvent('disconnect')  # 发送客户端断开通知到主界面
                    break
                print(data)
                self.sendClientDataToUi(data)
            except ConnectionResetError as reason:
                self.sendClientDataToUi("已经离开对话。")
                self.is_running = False
                self.emitConnectEvent('disconnect')  # 发送客户端断开通知到主界面
                break
        self.clientSocket.close()
        # self.serverSocket.close()
        # self.serverStatus.emit("服务已经关闭。")

    def send_data_to_client(self, message):
        try:
            self.clientSocket.send(message.encode("utf-8"))
        except Exception as reason:
            print("发送失败,原因:", reason)

    def stop(self):
        if self.is_running:
            self.is_running = False

    def emitConnectEvent(self, state):
        self.clientConnection.emit(state)

    def sendClientDataToUi(self, message):
        self.receivedClientData.emit(message)

  • 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
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

三、TCP client 完整代码

import socket
from PyQt6.QtCore import QThread, pyqtSignal


class TCPClient:

    def __init__(self, ui, ip, clientName, port):
        self.ui = ui
        self.ip = ip
        self.hostName = clientName
        self.port = port

        self.socket = None
        self.socketThread = None
        self.connect_server()

    def connect_server(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socketThread = TCPClientSocketReceiveThread(self.socket)
        self.socketThread.receivedServerData.connect(self.update_ui_chat_content)
        if self.connect_success(self.ip, self.port):
            self.socketThread.start()

    def update_ui_chat_content(self, serverMessage):
        self.ui.textEdit.append(serverMessage)

    def stop(self):
        self.socketThread.stop()

    def send_data(self, sentence):
        sentence = self.hostName + ":" + sentence
        self.ui.textEdit.append(sentence)
        self.socket.send(sentence.encode())

    def connect_success(self, ip, port):
        try:
            self.socket.connect((ip, port))
            return True
        except Exception as reason:
            print(reason)
            return False


class TCPClientSocketReceiveThread(QThread):
    receivedServerData: pyqtSignal = pyqtSignal(str)  # 向主线程发送接受到客户端的数据

    def __init__(self, clientSocket):
        super(TCPClientSocketReceiveThread, self).__init__()
        self.clientSocket = clientSocket
        self.is_running = True

    def stop(self):
        self.is_running = False
        self.clientSocket.close()

    def run(self):
        while self.is_running:
            try:
                msg = self.clientSocket.recv(1024).decode("utf-8")  # 接受服务端消息
                if not msg:
                    break
                self.receivedServerData.emit(msg)
            except Exception as reason:
                print(reason)
                break
        self.stop()
        self.receivedServerData.emit("已经与服务端断开。")

  • 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

四、截图

1、server端

在这里插入图片描述

2、第一个客户端

在这里插入图片描述

3、第二个客户端

在这里插入图片描述
可以单机多个客户端通讯,也可以在局域网测试。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/1013360
推荐阅读
相关标签
  

闽ICP备14008679号