-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_server.py
More file actions
82 lines (70 loc) · 2.79 KB
/
chat_server.py
File metadata and controls
82 lines (70 loc) · 2.79 KB
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
72
73
74
75
76
77
78
79
80
81
82
import socket
import select
from thread import *
import sys
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
"""
the first argument AF_INET is the address domain of the socket. This is used when we have an Internet Domain
with any two hosts
The second argument is the type of socket. SOCK_STREAM means that data or characters are read in a continuous flow
"""
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if len(sys.argv) != 2:
print "Correct usage: script, port number"
exit()
Port = int(sys.argv[1])
server.bind(('', Port))
# binds the server at the specified port number. The client must be aware of this parameter
server.listen(100)
print "server started and is listening"
# listens for 100 active connections. This number can be increased as per convenience
list_of_clients = []
list_of_questions = ["Question1?", "Question2?", "Question3?", "Question4?", "Question5?", ]
def clientthread(conn, addr):
ind=0
conn.send("Server: Welcome to this chatroom!\n")
print "welcome message sent to ","<" + addr[0] + "> "
conn.send(("Server: " + list_of_questions[ind]))
# sends a message to the client whose user object is conn
while True:
try:
if ind != 4:
ind+=1
else:
ind=0
message = conn.recv(2048)
if message:
print "<" + addr[0] + "> " + message
message_to_send = "<" + addr[0] + "> " + message
broadcast(message_to_send + "\n" + ("Server: " + list_of_questions[ind]), conn)
#prints the message and address of the user who just sent the message on the server terminal
else:
remove(conn)
except:
continue
def broadcast(message,connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()
print clients + " was removed"
remove(clients)
def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)
while True:
conn, addr = server.accept()
"""
Accepts a connection request and stores two parameters, conn which is a socket object for that user, and addr which contains
the IP address of the client that just connected
"""
list_of_clients.append(conn)
print addr[0] + " connected"
#maintains a list of clients for ease of broadcasting a message to all available people in the chatroom
#Prints the address of the person who just connected
start_new_thread(clientthread,(conn,addr))
#creates and individual thread for every user that connects
conn.close()
server.close()