# -*-coding:Latin-1 -*
"""client chat (TCP)"""
import socket
import time
import os

    
hote = raw_input("Ip (xxx.xxx.xxx.) > ")
if not hote:
    hote = "localhost"
port = 6660
connecte = False

           
print("Recherche du serveur...")

while connecte==False:
#en attente de la connexion
    i = 0
    while i < 256:
       print("{}{}".format(hote, i))
       try:
          cnn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          cnn.settimeout(0.01)
          essai_hote = "{}{}".format(hote, i)
          cnn.connect((essai_hote, port))
          connecte = True
          cnn.send("salut, voila ton ip: {}".format(essai_hote))
          message_recu = cnn.recv(1024)
          print(message_recu)   
          break
       except KeyboardInterrupt:
          break
       except:
          i += 1


if connecte == True:
   print("Connexion etablie avec le serveur a l'adresse {}".format(essai_hote))
os.system("pause")

