By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

msn client/serveur python

P: 5
Bonjour,
je suis bloqué ilya preque 2 semaines dans le code suivant. je veut que le client et le serveur envoyent leurs message d'une maniere comme msn. je crois qu'ilya quelque chose à ajouter dans les boucles.

d'abord code serveur:

from socket import *
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:
msgServeur = raw_input("S> ")
connexion.send(msgServeur)
msgClient = self.connexion.recv(1024)
if msgClient.upper() == "FIN" or msgClient =="":
break
message = "%s> %s" % (nom, msgClient)
print message



for cle in conn_client:
if cle != nom:
conn_client[cle].send(message)

self.connexion.close()
del conn_client[nom]
print "Client %s deconnecte." % nom
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mySocket.bind(('127.0.0.1', 84))
except socket.error:
print "La liaison du socket a l'adresse choisie a echoue."
sys.exit()
print "Stand by!!!!"
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 connecte, adresse IP %s, port %s." %\
(it, adresse[0], adresse[1])
connexion.send("Vous etes connecte. Envoyez vos messages.")

code client.


# Definition d"un client reseau gerant en parallele l'emission
# et la reception des messages (utilisation de 2 THREADS).

host = '127.0.0.1'
port = 84

import socket, sys, threading

class ThreadReception(threading.Thread):
"""objet thread gerant la reception des messages"""
def __init__(self, conn):
threading.Thread.__init__(self)
self.connexion = conn # ref. du socket de connexion

def run(self):
while 1:
message_recu = self.connexion.recv(1024)
print "*" + message_recu + "*"

print "Client arrete. Connexion interrompue."
self.connexion.close()

class ThreadEmission(threading.Thread):
"""objet thread gerant l'emission des messages"""
def __init__(self, conn):
threading.Thread.__init__(self)
self.connexion = conn # ref. du socket de connexion

def run(self):
while 1:
message_emis = raw_input()
self.connexion.send(message_emis)

# Programme principal - etablissement de la connexion :
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
connexion.connect((host, port))
except socket.error:
print "La connexion a echoue."
sys.exit()
print "Connexion etablie avec le serveur."

# Dialogue avec le serveur : on lance deux threads pour gerer
# independamment l'emission et la reception des messages :
th_E = ThreadEmission(connexion)
th_R = ThreadReception(connexion)
th_E.start()
th_R.start()
Mar 18 '08 #1
Share this Question
Share on Google+
1 Reply


micmast
100+
P: 144
C'est possible de mettre le code dans tag?

For those that don't speak french:

Hey,

I'm stuck, almost 2 weeks. I want the client and server to send message kinda like msn does it.
Mar 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.