-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
94 lines (75 loc) · 2.83 KB
/
Server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import socket
import thread
sHost = '192.168.12.174'
nPort = 22222
listClientSockets = []
def InformAboutAllConnectedClients():
print '\n\nAll connected clients: ' + str(len(listClientSockets))
for sock in listClientSockets:
print sock.getsockname()[0] + ':' + str(sock.getsockname()[1])
print '\n'
def SendMessageToClientsExceptSpecified(clientSocket, sMessage):
for curSocket in listClientSockets:
if clientSocket == curSocket:
continue
curSocket.send(sMessage)
#THREAD
def ThreadListenClient(socket , sIP, nPort):
'''
thread for receiving messages from each client
'''
print '\nCLIENT ' + sIP + ':' + str(nPort) + ' started thread for listening client'
socket.send('Thank you for connecting. I\'m server')
#print '-> Client ' + sIP + ':' + str(nPort) + ' Thank you for connecting. I\'m server'
while True:
sMessage = socket.recv(1024)
if sMessage == '':
print 'Socket ' + sIP + ':' + str(nPort) + ' was disconnected'
listClientSockets.remove(socket)
#InformAboutAllConnectedClients()
break
# prepating the message to everyone
sMessageToAllClients = '\nCLIENT ' + sIP + ':' + str(nPort) + ' said:\n' + sMessage + '\n'
print sMessageToAllClients
# send message to all clients, except current
SendMessageToClientsExceptSpecified (socket, sMessageToAllClients)
#THREAD
def ThreadWaitingNewClients():
'''
thread for wait new connections from Clients
'''
# Create a socket object
socketServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#host = socket.gethostname() # Get local machine name
#binds address (hostname, port number pair) to socket.
socketServer.bind((sHost, nPort))
# the maximum value is system-dependent (usually 5),
socketServer.listen(5)
while True:
print '\nSERVER: waiting for a new client at ' + sHost + ':' + str(nPort)
socketClient, ClientAddress = socketServer.accept() # Establish connection with client.
print 'New client connected - ' + ClientAddress[0] + ":" + str(ClientAddress[1])
#add client to member list
listClientSockets.append(socketClient)
#InformAboutAllConnectedClients()
#run respective thread for receiving messages from client
thread.start_new_thread(ThreadListenClient, (socketClient, ClientAddress[0], ClientAddress[1]))
def CloseAllSockets():
print '\n\nClose all connected clients: ' + str(len(listClientSockets))
for sock in listClientSockets:
sock.shutdown(1)
sock.close()
print '\nClosing Done'
# MAIN
#start thread for socket server
thread.start_new_thread(ThreadWaitingNewClients, ())
#loop for user input
while True:
sCommand = raw_input('\nenter command: \nq - exit ')
sCommand = sCommand.lower()
if 'q' == sCommand:
# user wants to shut down server - close all sockets
CloseAllSockets()
break
else:
print 'Wrong command'