-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
66 lines (54 loc) · 2.23 KB
/
Server.py
File metadata and controls
66 lines (54 loc) · 2.23 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
#Internet Chat Relay Utilities
#Import needed libraries
import socket, select
import Utilities
from Class import Lobby, Client
#Check to see if the script is being run directly or add as a module
if __name__ == '__main__':
#Server startup message
print 'Staring up the server'
#Create a TCP socket using IPv4
serverSocket = Utilities.createSocket()
#Setup the server socket for connection
serverSocket = Utilities.serverSetup(serverSocket)
#Display the connection list
Utilities.displayConnections()
#Create an lobby instance and initail connection list
lobby = Lobby()
while True:
#Handle the socket list functions during the connection
readClients, writeClients, errorSockets = select.select(Utilities.CONNECTION_LIST, [], [])
#Wait for socket to read
for client in readClients:
#Check for new connection
if client is serverSocket:
#Handle the new connection
sockfd, addr = serverSocket.accept()
#Create a new client instance
newClient = Client(sockfd)
#Add the new client to the connection list
Utilities.CONNECTION_LIST.append(newClient)
#Display new connection information
Utilities.newConnection(sockfd, addr)
#Display the connection list
Utilities.displayConnections()
#Send the welcome message
lobby.lobbyWelcome(newClient)
#Otherwise connection is from known socket
else:
#Receive the message
msg = client.socket.recv(Utilities.RECV_BUFFER)
#Check for a valid message
if msg:
#Handle the received message
lobby.handleMsg(client, msg.decode().lower())
else:
#Clean/close the socket connection
client.socket.close()
Utilities.deleteConnection(client)
continue
#Wait for socket error
for sock in errorSockets:
#Clean/close the socket connection
sock.close()
Utilities.deleteConnection(sock)