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
global num
global x
# 처음일때
global datalist
global datanum
global datanum2
while True:
if num < 17:
num += 1
txt = txtParts[num]
datanum += 1
datanum2 += 1
try:
data = socket.recv(1024)
txt['state'] = 'normal'
text = str(data.decode()).split('☈')
if first:
text[0].replace('☈', '').strip()
txt.delete(0, END)
txt.insert("end", text[0] + text[1])
datalist.append(text[0] + text[1])
first = False
else:
if str(text[0]) == str(name):
txt.config(justify='right')
txt.delete(0, END)
txt.insert("end", text[1])
datalist.append(text[1])
else:
text[0].replace('☈', '').strip()
txt.delete(0, END)
txt.insert("end", text[0])
datalist.append(text[0])
txtParts[num + 1]['state'] = 'normal'
txtParts[num + 1].delete(0, END)
txtParts[num + 1].insert("end", text[1])
datalist.append(text[1])
datanum += 1
datanum2 += 1
num += 1
txtParts[num]['state'] = 'disabled'
txt['state'] = 'disabled'
except ConnectionAbortedError as e:
txt['state'] = 'normal'
txt.delete(0, END)
txt.insert("end", '\n[System] 접속을 종료합니다.\n')
txt['state'] = 'disabled'
exit()
else:
# if num <= 0:
# num = 17
# else:
# num -= 1
txt = txtParts[17]
datanum += 1
datanum2 += 1
try:
data = socket.recv(1024)
txt['state'] = 'normal'
text = str(data.decode()).split('☈')
if first:
text[0].replace('☈', '').strip()
txt.delete(0, END)
datalist.append(text[0] + text[1])
go_down()
txt.insert("end", text[0] + text[1])
first = False
else:
if str(text[0]) == str(name):
txt.config(justify='right')
txt.delete(0, END)
datalist.append(text[1])
go_down()
txt.insert("end", text[0] + text[1])
else:
text[0].replace('☈', '').strip()
txt.delete(0, END)
datalist.append(text[0])
go_down()
txt.insert("end", text[0] + text[1])
num += 1
txtParts[num]['state'] = 'normal'
txtParts[num].delete(0, END)
datalist.append(text[1])
datanum += 1
datanum2 += 1
go_down()
txtParts[num]['state'] = 'disabled'
txt['state'] = 'disabled'
except ConnectionAbortedError as e:
txt['state'] = 'normal'
txt.delete(0, END)
txt.insert("end", '\n[System] 접속을 종료합니다.\n')
txt['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 go_up():
global datalist
global datanum2
# datanum2는 go, down에 따라 맨 밑(17번째칸)의 datalist값으로 바꿔야 함
if datanum2-18 != 0:
for i in range(17, -1, -1):
txtParts[i]['state'] = 'normal'
txtParts[i].config(justify='right')
# datanum2에 justify 값 넣기([][])
txtParts[i].delete(0, END)
txtParts[i].insert("end", datalist[datanum2-(2+i)])
datanum2 -= 1
def go_down():
global datalist
global datanum2
for i in range(0, 18):
txtParts[i]['state'] = 'normal'
txtParts[i].config(justify='right')
txtParts[i].delete(0, END)
txtParts[i].insert("end", datalist[datanum2-(17-i)])
txtParts[i]['state'] = 'disabled'
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)
start_new_thread(login, ())
frame = Frame(c_root)
upbtn = Button(frame, text='up', command=go_up)
upbtn.pack(fill='x')
chat_canvas = Canvas(frame)
chat_canvas.pack()
downbtn = Button(frame, text='down', command=go_down)
downbtn.pack(fill='x')
txtParts = []
for i in range(0, 18):
x = Entry(chat_canvas, width=65, disabledbackground='#BECDFF', disabledforeground='black')
x.pack()
x['state'] = 'disabled'
txtParts.append(x)
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')
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 = ''
num = -1
x = True
datalist = []
datanum = -1
downbool = False
upbool = False
datanum2 = -1
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()
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
2022.09.17 client
2022.09.17 client
댓글 0개
좋아요
댓글(0)
bottom of page