-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveur.py
130 lines (86 loc) · 2.52 KB
/
serveur.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from socket import *
import select
from time import time, ctime
import sys
import signal
#from Serveur_Scrabble import *
import threading as th
host = gethostname()
port = 8000
socketPrincipal = socket(AF_INET, SOCK_STREAM)
socketPrincipal.bind((host, port))
socketPrincipal.listen(4)
print 'en ecoute'
nbJoueur = int(raw_input('nombre de Joueur ? '))
verrou=th.Lock()
#####################################################################
# pour la fin de la boucle
def finir(listSock, listThread, socketPrincipal):
global continue_boucle_client
print 'commencer l\'apocalypse'
for i in listSock:
i.send('fin')
# i.close()
# i.shutdown(1)
print 'fin des client'
continue_boucle_client = False
for i in th.enumerate():
if i != th.currentThread():
i.join()
print 'fin des thread'
socketPrincipal.shutdown(1)
socketPrincipal.close()
print 'fin de la socket principal'
####################################################################
continue_boucle_client = True
####################################################################
def commence(newsocket):
# newsocket.send('commence serveur')
global continue_boucle_client
while continue_boucle_client:
data = newsocket.recv(2055)
print data
# si un client c'est arreter
if data == 'fin':
verrou.acquire()
continue_boucle_client = False
print 'fin de la connexion client TCP'
newsoc.remove(newsocket)
verrou.release()
# distribuer les informations aux autres
if data[:2] == 'tb':
verrou.acquire()
for i in newsoc:
if i != newsocket:
i.send(data)
verrou.release()
####################################################################
threads = [] # liste des threads
newsoc = [] # liste des sockets
##################################################################
try:
# En attente que tous les joueurs se mettent en place
while len(newsoc)<nbJoueur:
news, addr = socketPrincipal.accept()
print "connected from", addr
# liste des thread et des sockets
t = th.Thread(target=commence, args=(news,))
threads.append(t)
newsoc.append(news)
news.send('joueur %d'%len(newsoc))
t.start()
# si un client se deconecte alors le serveur s'arrete
while continue_boucle_client:
if continue_boucle_client==False:
break
# si on appuie sur ctrl-C
except KeyboardInterrupt:
print 'vous avez pressez ctrl+C'
except error:
print 'error'
# pour finir
finally:
finir(newsoc, threads, socketPrincipal)
print 'fin de connexion'
sys.exit(1)
#################################################################