server.py
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('127.0.0.1')
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.serveropenbtn = QPushButton('server open')
self.serveropenbtn.clicked.connect(self.serveropen)
self.serveropenbtn.setCheckable(False)
self.serveropenbtn.toggle()
self.serveropenbtn.setMaximumWidth(100)
self.hbox.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.hbox.addWidget(self.serverclosebtn)
self.hbox.addStretch(1)
self.vbox.addLayout(self.hbox)
self.mtext = QTextEdit()
self.vbox.addWidget(self.mtext)
self.setLayout(self.vbox)
self.setWindowTitle('server')
self.move(300, 300)
self.setFixedSize(QSize(600, 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_socket0
server_socket0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket0.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket0.bind((HOST, PORT))
server_socket0.listen()
# chat_log['state'] = 'normal'
self.mtext.setText('Server Start\n')
# chat_log['state'] = 'disabled'
while 1:
client_socket, addr = server_socket0.accept()
c_list0.append(client_socket)
start_new_thread(self.threaded, (c_list0[-1], addr))
def threaded(self, client_socket, addr):
global c_list0
# global chat_log
# chat_log['state'] = 'normal'
self.mtext.setText('Connected by :' + addr[0] + ':' + str(addr[1]) + '\n')
# chat_log['state'] = 'disabled'
for c in c_list0:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 접속하였습니다.').encode())
while 1:
try:
data = client_socket.recv(1024)
# chat_log['state'] = 'normal'
self.mtext.setText('Received from ' + addr[0] + ' : ' + str(addr[1]) + ' :: ' + str(data.decode()) + '\n')
# chat_log['state'] = 'disabled'
for c in c_list0:
c.sendall((str(addr[1]) + ' : ' + data.decode()).encode())
except ConnectionResetError as e:
c_list0.remove(client_socket)
for c in c_list0:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 나갔습니다.').encode())
# chat_log['state'] = 'normal'
self.mtext.setText('Disconnected by ' + addr[0] + ':' + str(addr[1]) + '\n')
# chat_log['state'] = 'disabled'
break
client_socket.close()
c_list0 = []
close = False
server_socket0 = None
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
client.py
import sys
import socket
import threading
from time import sleep
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('127.0.0.1')
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.loginbtn = QPushButton('login')
self.loginbtn.clicked.connect(self.try_login)
self.loginbtn.setCheckable(False)
self.loginbtn.toggle()
self.loginbtn.setMaximumWidth(100)
self.hbox.addWidget(self.loginbtn)
self.logoutbtn = QPushButton('logout')
self.logoutbtn.clicked.connect(self.try_logout)
self.logoutbtn.setCheckable(False)
self.logoutbtn.toggle()
self.logoutbtn.setMaximumWidth(100)
self.hbox.addWidget(self.logoutbtn)
self.vbox.addLayout(self.hbox)
self.msgtext = QTextEdit()
self.vbox.addWidget(self.msgtext)
self.hbox2 = QHBoxLayout()
self.writetxt = QTextEdit()
self.writetxt.setMaximumHeight(100)
self.hbox2.addWidget(self.writetxt)
self.vbox2 = QVBoxLayout()
self.filebtn = QPushButton('file')
self.filebtn.setCheckable(False)
self.filebtn.toggle()
self.vbox2.addWidget(self.filebtn)
self.sendbtn = QPushButton('send')
self.sendbtn.setCheckable(False)
self.sendbtn.toggle()
self.sendbtn.clicked.connect(self.set_go_send)
self.vbox2.addWidget(self.sendbtn)
self.hbox2.addLayout(self.vbox2)
self.vbox.addLayout(self.hbox2)
self.setLayout(self.vbox)
self.setWindowTitle('client')
self.move(300, 300)
self.setFixedSize(QSize(600, 500))
self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
self.show()
def send(self, socket):
global go_send
while True:
if go_send:
message = (self.writetxt.toPlainText()).rstrip()
socket.send(message.encode())
self.writetxt.setText('')
go_send = False
else:
if go_out:
socket.close()
exit()
sleep(0.1)
def receive(self, socket):
first = True
while True:
try:
data = socket.recv(1024)
if first: # 이걸 처음 체크 이후 의미없이 매번 체크하므로 이렇게 하는 건 효율적이지 않음.
self.msgtext.setText(str(data.decode()))
first = False
else:
self.msgtext.setText('\n' + str(data.decode()))
except ConnectionAbortedError as e:
self.msgtext.setText('\n[System] 접속을 종료합니다.\n')
exit()
def login(self):
# 서버의 ip주소 및 포트
HOST = self.iptxt.text()
PORT = int(self.porttxt.text())
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
threading.Thread(target=self.send, args=(client_socket,)).start()
threading.Thread(target=self.receive, args=(client_socket,)).start()
exit()
def try_login(self):
global go_out
start_new_thread(self.login, ())
# login_button['state'] = 'disabled'
# logout_button['state'] = 'active'
# ip_entry['state'] = 'readonly'
# port_entry['state'] = 'readonly'
go_out = False
def try_logout(self):
global go_out
# login_button['state'] = 'active'
# logout_button['state'] = 'disabled'
# ip_entry['state'] = 'normal'
# port_entry['state'] = 'normal'
go_out = True
def set_go_send(self, event):
global go_send
go_send = True
go_out, go_send = False, False
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
2022.9.3.
2022.9.3.
댓글 0개
좋아요
댓글(0)
bottom of page