-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_client.py
71 lines (54 loc) · 1.92 KB
/
new_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
from datetime import datetime
from socket import *
import threading
from object.user import User
from object.chat import Chat
class Client():
_user:User = User('None','None')
__socket=None
def __init__(self, user:User):
self._user=user
def connect_server(self, ip:str, port:int):
self.__socket=socket(AF_INET, SOCK_STREAM)
try:
self.__socket.connect((ip,port))
print(f'{ip}:{port} 연결되었습니다.')
except Exception as e:
print('연결 실패했습니다. 이유 : ')
print(e)
if __name__ == '__main__':
send_thread=threading.Thread(target=self.input_send, args=())
send_thread.start()
recv_thread=threading.Thread(target=self.recv_loop,args=())
recv_thread.start()
def input_send(self):
while True:
self.send(input())
def send(self, data:str):
chat=Chat(user=self._user, msg=data, time=datetime.now())
self.__socket.send(chat2json(chat).encode('utf-8'))
def recv_loop(self):
while True:
print(self.receive())
def receive(self) -> Chat:
recvData=self.__socket.recv(1024).decode('utf-8')
try:
chat=json2chat(recvData)
return chat
except Exception as e:
print('에러 발생. 이유 : ')
print(e)
return Chat(User('None','None'), '에러 발생', time=datetime.now())
def set_off(self):
self.__socket.close()
sys.exit()
def init_client():
client=Client(User('1','hb'))
_ip=input("ip : ")
_port=input("port : ")
client.connect_server(_ip, int(_port))
import atexit
atexit.register(lambda : client.set_off())
if __name__=='__main__':
init_client()