-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatroomclient.py
53 lines (35 loc) · 1.11 KB
/
chatroomclient.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
import socket
import threading
nickname=input('Enter your nickname: ')
disconnect_msg="disconnect"
HEADER=64
PORT=5050
SERVER=socket.gethostbyname(socket.gethostname())
ADDR=(SERVER, PORT)
FORMAT='utf-8'
SOCKETCLIENT=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SOCKETCLIENT.connect(ADDR)
def receive():
while True:
try:
firstmsg=SOCKETCLIENT.recv(2048).decode(FORMAT)
if firstmsg=='Enter your nickname: ':
SOCKETCLIENT.send(nickname.encode(FORMAT))
else:
print(firstmsg)
except:
print('Error happened')
SOCKETCLIENT.close()
break
def send():
while True:
usermsg=input()
msg_len=len(f'{nickname}: {usermsg}')
send_len=str(msg_len).encode(FORMAT)
SOCKETCLIENT.send(send_len)
# message=f'{nickname}: {usermsg}'
SOCKETCLIENT.send(f'{nickname}: {usermsg}'.encode(FORMAT))
recievethread=threading.Thread(target=receive)
recievethread.start()
typemsgthread=threading.Thread(target=send)
typemsgthread.start()