Ver Mensaje Individual
  #14 (permalink)  
Antiguo 06/01/2014, 07:17
roberkas98
 
Fecha de Ingreso: noviembre-2013
Mensajes: 61
Antigüedad: 10 años, 5 meses
Puntos: 0
Respuesta: Chat sencillo TCP

Cita:
Iniciado por razpeitia Ver Mensaje
Usando solo select y sockets.

Código Python:
Ver original
  1. import select
  2. import socket
  3. import sys
  4. import re
  5.  
  6.  
  7. CONNECTION_LIMIT = 10
  8. HOST, PORT = 'localhost', 8080
  9.  
  10. # Create a TCP/IP socket
  11. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. server.setblocking(0)
  13.  
  14. # Bind the socket to the port
  15. server_address = (HOST, PORT)
  16. print >>sys.stderr, 'starting up on %s port %s' % server_address
  17. server.bind(server_address)
  18.  
  19. # Listen for incoming connections
  20. server.listen(10)
  21.  
  22. # Sockets from which we expect to read
  23. inputs = [ server ]
  24.  
  25. # Sockets to which we expect to write
  26. outputs = [ ]
  27.  
  28. # User list
  29. users = {}
  30.  
  31. def broadcast(sockets, message):
  32.     for s in sockets:
  33.         s.send(message)
  34.  
  35. while True:
  36.     readable, writable, exceptional = select.select(inputs, outputs, inputs)
  37.    
  38.     # Handle inputs
  39.     for s in readable:
  40.         if s is server:
  41.             # A "readable" server socket is ready to accept a connection
  42.             connection, client_address = s.accept()
  43.             connection.setblocking(0)
  44.  
  45.             inputs.append(connection)
  46.             outputs.append(connection)
  47.  
  48.             users[connection] = "nameless"
  49.             username = users[connection]
  50.             broadcast(outputs, '%s has enter' % username)
  51.         else:
  52.             data = s.recv(1024)
  53.             if data:
  54.                 # A readable client socket has data
  55.                 data = data.strip()
  56.                 m = re.match(r'^/nick (\w+)$', data, re.I)
  57.                 if m:
  58.                     old_username = users[s]
  59.                     users[s] = m.group(1)
  60.                     data = '%s is now %s' % (old_username, users[s])
  61.                 else:
  62.                     data = "%s > %s" % (users[s], data)
  63.                 broadcast(outputs, data)
  64.             else:
  65.                 # Stop listening for input on the connection
  66.                 username = users[s]
  67.                 outputs.remove(s)
  68.                 inputs.remove(s)
  69.                 s.close()
  70.                 broadcast(outputs, '%s has left' % username)
  71.        
  72.  
  73.     # Handle "exceptional conditions"
  74.     for s in exceptional:
  75.         # Stop listening for input on the connection
  76.         inputs.remove(s)
  77.         outputs.remove(s)
  78.         s.close()

Código Python:
Ver original
  1. # client.py
  2. import socket
  3. import sys
  4. import select
  5. import Queue
  6.  
  7. HOST, PORT = 'localhost', 8080
  8. server_address = (HOST, PORT)
  9.  
  10. # Create a TCP/IP socket
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12.  
  13. # Connect the socket to the port where the server is listening
  14. print >>sys.stderr, 'connecting to %s port %s' % server_address
  15. sock.connect(server_address)
  16.  
  17.  
  18. inputs = [sys.stdin, sock]
  19. outputs = [sock]
  20. queue = Queue.Queue()
  21.  
  22. while True:
  23.     readable, writable, exceptional = select.select(inputs, outputs, inputs)
  24.  
  25.     for r in readable:
  26.         if r is sys.stdin:
  27.             message = sys.stdin.readline()
  28.             queue.put(message)
  29.         elif r is sock:
  30.             data = sock.recv(1024)
  31.             if not data:
  32.                 sock.close()
  33.                 print "Connection closed"
  34.                 sys.exit(0)
  35.             else:
  36.                 print data
  37.            
  38.     for w in writable:
  39.         if w is sock:
  40.             try:
  41.                 next_msg = queue.get_nowait()
  42.                 sock.send(next_msg)
  43.             except Queue.Empty:
  44.                 pass
  45.  
  46.     for ex in exceptional:
  47.         if ex is sock:
  48.             sock.close()

PD: Esto no funcionara en windows. El servidor si, pero el cliente no.


Por que no funcionaria el cliente?

Un saludo