-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatServer.py
47 lines (35 loc) · 1.21 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
import socket, select, string, sys
if __name__ == "__main__":
if(len(sys.argv) < 2) :
print 'Usage : program port'
sys.exit()
port = int(sys.argv[1]) #Read input from Command line argument
CLIENT_LIST = [] #List to maintain connected clients
# Create and Bind the UDP socket
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', port)
server_socket.bind(server_address)
print 'Server Initialized on %s port %s' % server_address
except:
print 'Error in creating/binding socket'
sys.exit()
while True:
try:
#Recieve data and maintain client list
data, address = server_socket.recvfrom(4096)
if 'greeting' in str(data):
#register the client as active for broadacst of further message
if address not in CLIENT_LIST:
CLIENT_LIST.append(address)
print "<" + str(address) + "> " + str(data)
else:
#print any the message from client on server
broadcast_message = "<" + str(address) + "> " + str(data)
print broadcast_message
#broadcast message to all active clients .
for client in CLIENT_LIST:
server_socket.sendto(broadcast_message, client)
except:
pass
server_socket.close()