client.py
import socket
from _thread import *
import threading
from tkinter import *
from time import sleep
c_list = []
close = False
server_socket = None
def log_in():
def sendname(socket):
global namebool
global name
name = nametxt.get()
socket.send(name.encode())
namebool = False
def send(socket):
global go_send
while True:
if go_send:
message = (message_input.get(1.0, "end")).rstrip()
socket.send(message.encode())
message_input.delete(1.0, "end")
go_send = False
else:
if go_out:
socket.close()
exit()
sleep(0.1)
def receive(socket):
global space
global name
first = True
while True:
try:
data = socket.recv(1024)
chat_log['state'] = 'normal'
text = str(data.decode()).split('☈')
if first:
text[0].replace('☈', '').strip()
chat_log.insert("end", text[0] + text[1])
first = False
else:
if str(text[0]) == str(name):
msgnum = 65 - int(len(text[1]))
for i in range(msgnum):
# space = space + str(i % 10)
space = space + ' '
chat_log.insert("end", '\n' + '\n' + space + text[1])
space = ''
else:
text[0].replace('☈', '').strip()
chat_log.insert("end", '\n' + '\n' + text[0] + '\n' + text[1])
chat_log.see('end')
chat_log['state'] = 'disabled'
except ConnectionAbortedError as e:
chat_log['state'] = 'normal'
chat_log.insert("end", '\n[System] 접속을 종료합니다.\n')
chat_log['state'] = 'disabled'
exit()
def login():
# 서버의 ip주소 및 포트
HOST = iptxt.get()
PORT = int(porttxt.get())
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
threading.Thread(target=sendname, args=(client_socket,)).start()
threading.Thread(target=send, args=(client_socket,)).start()
threading.Thread(target=receive, args=(client_socket,)).start()
exit()
def set_go_send(event):
global go_send
go_send = True
c_root = Toplevel(root)
c_root.geometry('500x525')
c_root.title('client')
c_root.resizable(False, False)
c_root['bg'] = '#BECDFF'
start_new_thread(login, ())
chat_frame = Frame(c_root)
scrollbar = Scrollbar(chat_frame)
scrollbar.pack(side='right', fill='y')
chat_log = Text(chat_frame, width=65, height=30, state='disabled', yscrollcommand=scrollbar.set)
chat_log.pack(side='left')
scrollbar['command'] = chat_log.yview
chat_frame.place(x=10, y=10)
message_frame = Frame(c_root)
message_input = Text(message_frame, width=57, height=7)
message_input.pack(side='left')
send_button = Button(message_frame, text='보내기', bg='white', command=lambda: set_go_send(None))
send_button.pack(side='right', fill='y')
send_button['bg'] = 'white'
message_frame.place(x=10, y=415)
# message_input.bind("<Return>", set_go_send)
c_root.mainloop()
go_out, go_send = False, False
root = Tk()
root.resizable(False, False)
root.title('로그인')
root.geometry("300x125-500+200")
namebool = True
space = ''
iplbl = Label(root, text="server IP", width=10)
iplbl.grid(row=0, column=0)
iptxt = Entry(root, width=20)
iptxt.insert(0, '127.0.0.1')
iptxt.grid(row=0, column=1)
portlbl = Label(root, text="port", width=10)
portlbl.grid(row=1, column=0)
porttxt = Entry(root, width=20)
porttxt.insert(0, '9999')
porttxt.grid(row=1, column=1)
namelbl = Label(root, text="이름", width=10)
namelbl.grid(row=2, column=0)
nametxt = Entry(root, width=20)
nametxt.grid(row=2, column=1)
btnlogin = Button(root, text="로그인", command=log_in)
btnlogin.grid(row=3, column=0, columnspan=2, sticky=W + E + N + S)
root.mainloop()
server.py
import socket
from _thread import *
from tkinter import *
def threaded(client_socket, addr):
global chat_log
global namebool
while namebool == True:
data = client_socket.recv(1024)
chat_log['state'] = 'normal'
name = str(data.decode())
namebool = False
chat_log['state'] = 'normal'
chat_log.insert("end", '[System] ' + name +' 님이 접속하였습니다.'+'\n')
chat_log['state'] = 'disabled'
for c in c_list:
c.sendall(('[System] ' + '☈' + name + ' 님이 접속하였습니다.').encode())
while 1:
try:
data = client_socket.recv(1024)
chat_log['state'] = 'normal'
chat_log.insert("end", name + ':' + '\n' + str(data.decode()) + '\n' + '\n')
chat_log['state'] = 'disabled'
for c in c_list:
c.sendall((name + '☈' + data.decode()).encode())
except ConnectionResetError as e:
c_list.remove(client_socket)
for c in c_list:
c.sendall(('[System] ' + name + ' 님이 나갔습니다.').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()
def server_open():
HOST = ip_entry.get()
PORT = int(port_entry.get())
start_new_thread(make_server, (HOST, PORT))
open_button['state'] = 'disabled'
ip_entry['state'] = 'readonly'
port_entry['state'] = 'readonly'
def server_close():
exit()
def make_server(HOST, PORT):
global server_socket
global namebool
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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()
namebool = True
c_list.append(client_socket)
start_new_thread(threaded, (c_list[-1], addr))
c_list = []
close = False
server_socket = None
namebool = True
s_root = Tk()
s_root.geometry('500x500')
s_root.title('Server')
s_root.resizable(False, False)
''' Top Menu '''
Label(s_root, text='Server IP : ').place(x=20, y=20)
Label(s_root, text='Port : ').place(x=250, y=20)
ip_entry = Entry(s_root, width=14)
ip_entry.place(x=83, y=21)
ip_entry.insert(0, '127.0.0.1')
port_entry = Entry(s_root, width=5)
port_entry.place(x=290, y=21)
port_entry.insert(0, '9999')
open_button = Button(s_root, text='Server Open', command=server_open)
open_button.place(x=380, y=18)
''' Middle Menu '''
chat_log = Text(s_root, width=65, height=29, state='disabled', spacing2=2)
chat_log.place(x=20, y=60)
''' Bottom Menu '''
close_button = Button(s_root, text='Server Close', command=server_close)
close_button.place(x=200, y=460)
s_root.mainloop()
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
2022.09.17.
2022.09.17.
댓글 0개
좋아요
댓글(0)
bottom of page