Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import string
  2. HOST = '' # Endereco IP do Servidor
  3. PORT = 51515 # Porta que o Servidor esta
  4. tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. orig = ('',PORT)
  6. tcp.bind(orig)
  7. tcp.listen(1)
  8. i = 0
  9. while True:
  10. con, cliente = tcp.accept()
  11. print 'Conectado por', cliente
  12. while True:
  13. con.settimeout(100)
  14. try:
  15. sinal = con.recv(1024)
  16. except socket.timeout:
  17. print >> sys.stderr, 'Tempo esgotado. Fechando conexao!'
  18. con.close()
  19. break
  20.  
  21. if not sinal: break
  22. if sinal == '+':
  23. i_prox = (i + 1) % 1000
  24. elif sinal == '-':
  25. i_prox = (i - 1) % 1000
  26. else:
  27. print >> sys.stderr, 'Entrada incorreta. Fechando conexao!'
  28. con.close()
  29. break
  30.  
  31. htons_i = socket.htons(i_prox)
  32. pack_i = struct.pack(">I", htons_i)
  33.  
  34. con.send(pack_i)
  35.  
  36. con.settimeout(100)
  37. try:
  38. conf = con.recv(1024)
  39. except socket.timeout:
  40. print >> sys.stderr, 'Tempo esgotado. Fechando conexao!'
  41. con.close()
  42. break
  43.  
  44. num_conf = string.atoi(conf)
  45.  
  46. if i_prox == num_conf:
  47. i = i_prox
  48.  
  49. con.close()
  50. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement