from Tkinter import *
import socket, sys, threading

HOST = "127.0.0.1"
PORT = 4578


class GestionEmition (threading.Thread):
	def __init__(self, sock):
		threading.Thread.__init__(self)
		self.connexion = sock
	
	def run(self):
		while 1:
			DATA_SERV= self.connexion.recv(1024)
			print 'Server -> ', DATA_SERV
			if message == '' or DATA_SERV.upper() == "FIN":
				break
			print'Client arrété, Connexion intérrompue'
			self.connexion.close
			
class GestionReception (threading.Thread):
	def __init__(self, sock):
		threading.Thread.__init__(self)
		self.connexion = sock
	
	def run(self):
		while 1:
			DATA_CLI = root.mess.get()
			self.connexion.send(DATA_CLI)

def GoConnect():
	reseau = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	conn = reseau.connect((HOST,PORT))
	print 'Connexion OK'
	DATA = reseau.recv(1024)
	print "Recption de bienvenue : ", DATA
	reseau.send('KiKooo')
	print "Envoi d'un message..."
	#try:
	#	conn, addr = reseau.connect((HOST,PORT))
	#	print'Connexion établie sur ', addr
	#except:
	#	print'Connexion échouée ...'
	#	sys.exit
	emition = GestionEmition(reseau)
	reception = GestionReception(reseau)
	emition.start()
	reception.start()
	
class mainapp(Tk):
	def __init__(self):
		Tk.__init__(self)
		root = self
		Label(root,text='IP du serveur :').grid(row=1,column=1)
		HOST = Entry(root)
		HOST.grid(row=1,column=2)
		Label(root,text='PORT du serveur :').grid(row=1,column=3)
		PORT = Entry(root)
		PORT.grid(row=1,column=4)
		Button(root,text='Connexion', command=GoConnect).grid(row=1,column=5)
		board = Text(root)
		board.grid(row=2,column=1,columnspan=5)
		mess = Entry(root)
		mess.grid(row=3,column=1,columnspan=4)
		Button(root,text='Send').grid(row=3,column=5)


root = mainapp()
root.mainloop()

