Nous avançons également nous aussi de notre côté. En effet, nous rencontrons également des problèmes avec le serveur et client . Nous pouvons envoyer nos messages par le client et les recevoir avec le serveur (que je vous joins). Notre soucis est que nous voudrions tout mettre dans la même fenêtre. Nous rajouterons aussi notre programme de cryptage/decryptage par la suite.
Serveur:
Client:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 HOST = '' PORT = 1234 import socket, sys, threading class ThreadClient(threading.Thread): def __init__(self, conn): threading.Thread.__init__(self) self.connexion = conn def run(self): nom = self.getName() while 1: msgClient = self.connexion.recv(1024).decode("Utf8") if not msgClient or msgClient.upper() =="FIN": break message = "%s> %s" % (nom, msgClient) print(message) for cle in conn_client: if cle !=nom: conn_client[cle].send(message.encode("Utf8")) self.connexion.close() del conn_client[nom] print("Client %s déconnecté." % nom) mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: mySocket.bind((HOST, PORT)) except socket.error: print("La liaison du socket à l'adresse choisie a échoué.") sys.exit() print("Serveur prêt, en attente de requêtes ...") mySocket.listen(5) conn_client={} while 1: connexion, adresse = mySocket.accept() th=ThreadClient(connexion) th.start() it = th.getName() conn_client[it]=connexion print("Client %s connecté, adresse IP %s, port %s." %\ (it, adresse[0], adresse[1])) msg ="Vous êtes connecté. Envoyer vos messsages." connexion.send (msg.encode("Utf8"))
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 host = '127.0.0.1' port=1234 import socket,sys,threading class ThreadReception(threading.Thread): def __init__(self,conn): threading.Thread.__init__(self) self.connexion = conn def run(self): while 1: message_recu = self.connexion.recv(1024).decode("Utf8") print("*" + message_recu + "*") if not message_recu or message_recu.upper() == "FIN": break th_e._stop() print("client arreté. connexion interrompue") self.connexion.close() class ThreadEmission (threading.Thread): def __init__ (self, conn): threading.Thread.__init__(self) self.connexion=conn def run(self): while 1: message_emis = input() self.connexion.send(message_emis.encode("Utf8")) connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: connexion.connect((host,port)) except socket.error: print("La connexion a échoué") sys.exit() print("connexion établie") th_E = ThreadEmission(connexion) th_R = ThreadReception(connexion) th_E.start() th_R.start()
Partager