<span style="color: rgb(204, 0, 0);"><b><span style="color: rgb(0, 0, 0);">Hola gente.</span></b><br style="color: rgb(0, 0, 0);"><br><span style="color: rgb(0, 0, 0);">Sigo luchando con la red y la cosa está difícil de verdad, así que recurro a la lista por una manito ya que cada vez estoy más perdido en el tema.</span><br style="color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);">A ver si hay un alma caritativa en la lista que se apiade de mi y me tire unas lineas de código para destrancarme en el tema.<br>En particular a Andres Ambrois si anda por acá, le hago el desafío (amistoso), ya que es quien maneja generalmente estos temas en la lista y seguramente pueda construirme rápidamente un sencillo ejemplo que funcione, de todas maneras, si alguien más a parte de Andres sabe de este tema, agradecería un ejemplo funcional.<br>
<br><b>Explico:</b><br>Según todo lo que he leído, para hacer un juego en red salvo raras excepciones debe utilizarse UDP (por diferentes motivos).<br>Así que en el juego que estoy haciendo, necesito que un jugador cree el juego en la red y los demás se conecten a él.<br>
Luego debo tener una lista de todos los jugadores dentro del juego para poder enviarles los datos y además asignar puntajes y demás.<br>Y por último, actualizarlos a todos cada cierto tiempo, pero mientras tanto tengo que mantener el servidor levantado en escucha por si alguien más se une al juego.<br>
<br>Así que implementé un servidor UDP que escucha permanentemente a los nuevos jugadores que se unen al juego, el problema es que nunca pasa de ahí, es decir queda siempre esperando nuevas conexiones y el juego nunca se inicia. El juego debiera iniciarse aunque solo estuviera el jugador local en él y cuando se conectara otro, este debiera aparecer en el juego, poder jugar ambos y así por siempre. He probado de todo un poco, de hecho sobre TCP funciona pero solo con 2 jugadores, de todas maneras ese es otro tema, también probé múltiples formas multihilo y ninguna funciona como teóricamente debiera (o según lo que yo entendí).<br>
<br><b>Lo que busco es: </b><br></span></span><ul><li><span style="color: rgb(204, 0, 0);"><span style="color: rgb(0, 0, 0);">Jugar y escuchar al mismo tiempo.</span></span></li><li><span style="color: rgb(204, 0, 0);"><span style="color: rgb(0, 0, 0);">Mantener en correspondencia cada conexión con sus objetos en el juego (y otras cosas que vendrán después de solucionar estas).</span></span></li>
</ul><span style="color: rgb(204, 0, 0);"><span style="color: rgb(0, 0, 0);">Dejo ejemplo básico de mi servidor y más abajo el del cliente básico para él.<br></span><br><span style="color: rgb(51, 51, 255);">#!/usr/bin/env python</span></span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"># -*- coding: utf-8 -*-</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">import SocketServer</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">import gobject</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">import sys</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">import time</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">import datetime</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">HOST, PORT = &quot;localhost&quot;, 9999</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);">
<b><span style="color: rgb(204, 0, 0);">class Servidor():</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">    def __init__(self):</span></b><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">        self.server = None</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">        self.get_server()</span><span style="color: rgb(204, 0, 0);"></span><br style="color: rgb(204, 0, 0);">
<b><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">    def get_server(self):</span></b><br style="color: rgb(204, 0, 0);"><span style="color: rgb(51, 51, 255);">    # Levanta el Servidor</span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(51, 51, 255);">        # <a href="http://docs.python.org/library/socketserver.html">http://docs.python.org/library/socketserver.html</a></span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">        # 20.17.2. Server Objects</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">        self.server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler) <span style="color: rgb(51, 51, 255);"># Instancia de Servidor UDP con su controlador</span></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        self.server.serve_forever() <span style="color: rgb(51, 51, 255);"># handle_request() o serve_forever() para procesar uno o muchos pedidos.</span></span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><b><span style="color: rgb(204, 0, 0);">class MyUDPHandler(SocketServer.BaseRequestHandler):</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">    def handle(self):</span></b><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">        print &quot;Conexion establecida con: &quot; , self.client_address</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        data = self.request[0].strip() <span style="color: rgb(51, 51, 255);"># strip elimina espacios vacios al inicio y al final de una cadena</span></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        socket = self.request[1]</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">        socket.sendto(&quot;ok&quot;, self.client_address)</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        </span><br style="color: rgb(204, 0, 0);"><b><span style="color: rgb(204, 0, 0);">    def setup(self):</span></b><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">    <span style="color: rgb(51, 51, 255);"># Llamado antes de la handle() para realizar acciones de inicialización necesaria. La implementación predeterminada no hace nada.</span></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        print &quot;Configurando Conección&quot;</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><b><span style="color: rgb(204, 0, 0);">    def finish(self):</span></b><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">    <span style="color: rgb(51, 51, 255);"># Llamado despues de la handle() para realizar cualquier método de limpieza. La implementación predeterminada no hace nada.</span></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">        print &quot;Finalizando Solicitud&quot;</span><br style="color: rgb(204, 0, 0);"><b><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">if __name__ == &quot;__main__&quot;:</span></b><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">    Servidor()</span><br><br><b>Cliente:</b><br><br style="color: rgb(204, 0, 0);"><span style="color: rgb(51, 51, 255);">#!/usr/bin/env python</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);"># -*- coding: utf-8 -*-</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">import socket</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">import sys</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">HOST, PORT = &quot;localhost&quot;, 9999</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"></span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">data = &quot;Envio del cliente&quot;</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"></span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">sock.sendto(data, (HOST, PORT))</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">recibido = sock.recv(1024)</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">print &quot;Enviado:     %s&quot; % data</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">print &quot;</span><span style="color: rgb(204, 0, 0);">Recibido</span><span style="color: rgb(204, 0, 0);">: %s&quot; % </span><span style="color: rgb(204, 0, 0);">recibido</span>