import sys
import socket
from tkinter import *
from _thread import *
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QVBoxLayout, QHBoxLayout, QScrollArea, \
QPushButton
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.vbox = QVBoxLayout()
self.hbox = QHBoxLayout()
self.hbox.addStretch(1)
self.iplbl = QLabel('Server ip:')
self.hbox.addWidget(self.iplbl)
self.iptxt = QLineEdit('14.33.178.245')
self.hbox.addWidget(self.iptxt)
self.iptxt.setMaximumWidth(100)
self.hbox.addStretch(1)
self.portlbl = QLabel('port:')
self.hbox.addWidget(self.portlbl)
self.porttxt = QLineEdit('9999')
self.porttxt.setMaximumWidth(50)
self.hbox.addWidget(self.porttxt)
self.hbox.addStretch(1)
self.vbox.addLayout(self.hbox)
self.scrArea = QScrollArea()
self.vbox.addWidget(self.scrArea)
self.hbox2 = QHBoxLayout()
self.serveropenbtn = QPushButton('server open')
self.serveropenbtn.clicked.connect(self.serveropen)
self.serveropenbtn.setCheckable(False)
self.serveropenbtn.toggle()
# self.serveropenbtn.setMaximumWidth(100)
self.hbox2.addWidget(self.serveropenbtn)
self.serverclosebtn = QPushButton('Server close')
self.serveropenbtn.clicked.connect(self.serverclose)
self.serverclosebtn.setCheckable(False)
self.serverclosebtn.toggle()
# self.serverclosebtn.setMaximumWidth(100)
self.hbox2.addWidget(self.serverclosebtn)
self.vbox.addLayout(self.hbox2)
self.setLayout(self.vbox)
self.setWindowTitle('server')
self.move(300, 300)
self.setFixedSize(QSize(500, 500))
self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
self.show()
def serveropen(self):
HOST = self.iptxt.text()
PORT = int(self.porttxt.text())
start_new_thread(self.make_server, (HOST, PORT))
self.serveropenbtn['state'] = 'disabled'
self.iptext['state'] = 'readonly'
self.porttxt['state'] = 'readonly'
def serverclose(self):
exit()
def make_server(self, HOST, PORT):
global server_socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 포트사용중이라 연결할 수 없다는 WinError 10048 에러를 해결하기 위해 필요합니다.
# 서버 소켓의 SOL_SOCKET의 SO_REUSEADDR(이미 사용중인 포트에 대해서도 바인드 허용) 를 1(True)로 설정하는 것으로 이해
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen()
chat_log['state'] = 'normal'
chat_log.insert("end", 'Server Start\n')
chat_log['state'] = 'disabled'
while 1:
client_socket, addr = server_socket.accept()
c_list.append(client_socket)
start_new_thread(self.threaded, (c_list[-1], addr))
def threaded(self, client_socket, addr):
global chat_log
chat_log['state'] = 'normal'
chat_log.insert("end", 'Connected by :' + addr[0] + ':' + str(addr[1]) + '\n')
chat_log['state'] = 'disabled'
for c in c_list:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 접속하였습니다.').encode())
while 1:
try:
data = client_socket.recv(1024)
chat_log['state'] = 'normal'
chat_log.insert("end",
'Received from ' + addr[0] + ' : ' + str(addr[1]) + ' :: ' + str(data.decode()) + '\n')
chat_log['state'] = 'disabled'
for c in c_list:
c.sendall((str(addr[1]) + ' : ' + data.decode()).encode())
except ConnectionResetError as e:
c_list.remove(client_socket)
for c in c_list:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 나갔습니다.').encode())
chat_log['state'] = 'normal'
chat_log.insert("end", 'Disconnected by ' + addr[0] + ':' + str(addr[1]) + '\n')
chat_log['state'] = 'disabled'
break
client_socket.close()
c_list = []
close = False
server_socket = None
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
채팅(pyqt5)
채팅(pyqt5)
댓글 0개
좋아요
댓글(0)
bottom of page