-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_client.py
60 lines (49 loc) · 1.44 KB
/
chat_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
'''
chat room 客户端
发送请求,展示结果
'''
import os,sys
from socket import *
ADDR = ('127.0.0.1',4532)
def send_msg(s,name):
while True :
try:
text = input('>>')
except (KeyboardInterrupt,SyntaxError ):
text = 'quit'
if text.strip()=='quit': ###除去可能有的空格
msg = 'Q'+name
s.sendto(msg.encode(), ADDR)
sys.exit('退出聊天室') ###只是退出了一个进程,不影响另一个进程,需要服务器将另一个进程退出
msg = 'C %s %s'%(name,text)
s.sendto(msg.encode(),ADDR)
def recv_msg(s):
while True :
data,addr = s.recvfrom(4096)
if data.decode() == 'Exit':
sys.exit() ###exit有break的作用吗??????
print(data.decode()+'\n>>',end = '')
def main():
s = socket(AF_INET,SOCK_DGRAM )
#进入聊天室
while True :
name = input('请输入昵称:')
msg = 'L '+name
s.sendto(msg.encode() ,ADDR)
#接受反馈
data, addr = s.recvfrom(2014)
if data.decode() == "OK":
print('您已进入聊天室')
break
else:
print(data.decode() )
#已经进入聊天室
pid = os.fork()
if pid <0:
sys.exit('Error')
elif pid==0:
send_msg(s,name)
else:
recv_msg(s)
if __name__ == '__main__': ###main Tab
main()