打开frp端口映射到外网,如果不需要外网此步可以省。
运行服务器代码,服务端部分ok。
开启后显示连接服务器成功若服务端未开启会报错这里没写错误处理程序。
服务端会显示客户端连接。
输入昵称登录。
互相群发消息。
服务器显示内容。
客户端下线。
import sys
import socket
import threading
import char_mainwin
from PyQt5.QtWidgets import QApplication,QMainWindow
name=None
class char_signal(char_mainwin.Ui_mainWindow):
def onClick(self):
global name
if self.lineEdit.text()+"None"!="None":
if self.pushButton.text()=="登入":
name=self.lineEdit.text()
self.pushButton.setText("已登入")
elif self.pushButton.text()=="已登入":
name=None
self.pushButton.setText("登入")
def sendOut(self):
if name!=None and self.lineEdit_2.text()+"None"!="None":
news=name+':'+self.lineEdit_2.text()
s.send(news.encode("gb2312"))
def textRes(self):
text=self.plainTextEdit.toPlainText()
self.plainTextEdit.setPlainText(text)
def setupUi(self, mainWindow):
char_mainwin.Ui_mainWindow.setupUi(self, mainWindow)
self.pushButton.clicked.connect(self.onClick)
self.pushButton_2.clicked.connect(self.sendOut)
self.plainTextEdit.blockCountChanged.connect(self.textRes)
def newThread(self):
while True:
receive = s.recv(1024).decode(encoding='gb2312')
self.plainTextEdit.appendPlainText(receive)
if __name__=='__main__':
s = socket.socket()
app=QApplication(sys.argv)
s.connect(('111.229.97.45', 1027)) #服务器ip+端口号
main=QMainWindow()
ui=char_signal()
ui.setupUi(main)
main.show()
t = threading.Thread(target=ui.newThread)
t.setDaemon(True)
t.start()
sys.exit(app.exec_())
QTDesigner生成代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'char_mainwin.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(404, 379)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(20, 10, 361, 361))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit.setMouseTracking(True)
self.lineEdit.setAcceptDrops(True)
self.lineEdit.setFrame(True)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.layoutWidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.verticalLayout.addWidget(self.plainTextEdit)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
mainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "聊天"))
self.label.setText(_translate("mainWindow", "昵称:"))
self.pushButton.setText(_translate("mainWindow", "登入"))
self.pushButton_2.setText(_translate("mainWindow", "发送"))
import socket
from threading import Thread
address=('192.168.31.32',1234)
g_socket_server=None
g_conn_pool=[]
def init():
global g_socket_server
g_socket_server = socket.socket()
g_socket_server.bind(address)
g_socket_server.listen(5)
print('服务器已启动,等待客户端连接...')
def accept_client():
while True:
client,addr=g_socket_server.accept()
print('IP:',addr)
g_conn_pool.append(client)
thread=Thread(target=message_handle,args=(client,))
thread.setDaemon(True)
thread.start()
def message_handle(client):
client.sendall('连接服务器成功!'.encode(encoding='gb2312'))
while True:
bytes=client.recv(1024)
print('客户端消息:',bytes.decode('gb2312'))
if len(bytes)==0:
client.close()
g_conn_pool.remove(client)
print('有一个客户端下线了。')
break
g_conn_pool[1].sendall(bytes)
g_conn_pool[0].sendall(bytes)
if __name__=='__main__':
init()
thread=Thread(target=accept_client)
thread.setDaemon(True)
thread.start()
while True:
index,msg=input('请输入“索引,消息”的形式:\n').split(',')
g_conn_pool[int(index)].sendall(msg.encode(encoding='gb2312'))