-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatServer.py
65 lines (55 loc) · 1.82 KB
/
ChatServer.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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import socket
host = "127.0.0.1"
port = 9921
addr = host,port
clients = {}
def stringPolishing(value):
length = 6-len(value)
for i in range(length):
value = '0'+value
return value
def decoderData(value,sock):
position = 0
print value
try:
commandLen = int(value[0:6])
position += 6
command = value[position:position+commandLen]
print 'command',command
position += commandLen
if command=="login":
nameLen = int(value[position:position+6])
position += 6
userName = value[position:position+nameLen]
print 'userName'+userName
sendData = ''
for key in clients.keys():
sendData += stringPolishing(str(len(key)))
sendData += key
clients[key].send('000005login'+stringPolishing(str(nameLen))+userName)
sock.send('000010currPeople'+stringPolishing(str(len(clients)))+sendData)
clients[userName] = sock
elif command=="talk":
for key in clients.keys():
clients[key].send(value)
elif command=="logout":
nameLen = int(value[position:position+6])
position += 6
userName = value[position:position+nameLen]
clients[userName].close()
del clients[userName]
for key in clients.keys():
clients[key].send(value)
except ValueError:
sock.send("command error")
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(addr)
server.listen(999)
while True:
clientSock,addrs = server.accept()
print "client ip:" + str(addrs[0])
print "client port:" + str(addrs[1])
clientData = clientSock.recv(2000)
decoderData(clientData,clientSock)