Exemplo n.º 1
0
        public bool Crear_Conexion(string ip, int puerto, ServerForm form)
        {
            try
            {
                IPAddress.TryParse(ip, out _ip_address);

                _puerto = puerto;

                _form = form;

                _mensajeManager = new MensajeManager();

                _servidor = new TcpListener(_ip_address, _puerto);

                _servidor.Start();

                _form.Imprimir_Log("Conexión abierta.");

                _juego = new Juego();

                _servidor.BeginAcceptTcpClient(AceptandoCliente, _servidor);

                _form.Imprimir_Log("Esperando clientes...");


                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.ToString());
                return(false);
            }
        }
Exemplo n.º 2
0
        private void AceptandoCliente(IAsyncResult AsyncResult)
        {
            _servidor = (TcpListener)AsyncResult.AsyncState;
            TcpClient Cliente_Entrante = null;
            Jugador   _jugador;

            try
            {
                Cliente_Entrante = _servidor.EndAcceptTcpClient(AsyncResult);
                _servidor.BeginAcceptTcpClient(AceptandoCliente, _servidor); //Sigue el loop para aceptar mas clientes.

                _form.Imprimir_Log("Recibiendo información de un cliente...");

                contador_clientes++;

                _jugador         = new Jugador(contador_clientes);
                _jugador.Cliente = Cliente_Entrante;


                lock (_juego.Jugadores)
                {
                    _juego.Jugadores.Add(_jugador);
                }

                _jugador.Lectura = new byte[512];

                _jugador.Cliente.GetStream().BeginRead(_jugador.Lectura, 0, _jugador.Lectura.Length, RecibiendoMensajeCliente, _jugador.Cliente);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 3
0
        private void AceptandoCliente(IAsyncResult AsyncResult)
        {
            _servidor = (TcpListener)AsyncResult.AsyncState;
            TcpClient Cliente_Entrante = null;
            Jugador   _jugador;

            try
            {
                if (_servidor == null || _servidor.Server == null)
                {
                    return;
                }

                Cliente_Entrante = _servidor.EndAcceptTcpClient(AsyncResult);
                _servidor.BeginAcceptTcpClient(AceptandoCliente, _servidor); //Sigue el loop para aceptar mas clientes.


                _jugador         = new Jugador();
                _jugador.Cliente = Cliente_Entrante;

                if ((_juego.JugadoresConectados.Count + 1) > _juego.CantidadJugadores)
                {
                    _jugador.EnviarMensaje(new Servidor_RechazarJugador(_juego));
                    return;
                }

                if (_juego.Iniciado)
                {
                    //enviar rechazo
                    return;
                }


                _form.Imprimir_Log("Recibiendo información de un cliente...");


                lock (_juego.JugadoresConectados)
                {
                    _juego.JugadoresConectados.Add(_jugador);
                }

                _jugador.Lectura = new byte[512];

                // _jugador.Cliente.GetStream().BeginRead(_jugador.Lectura, 0, _jugador.Lectura.Length, RecibiendoMensajeCliente, _jugador.Cliente);

                _jugador.Cliente.Client.BeginReceive(_jugador.Lectura, 0, _jugador.Lectura.Length, 0, new AsyncCallback(RecibiendoMensajeCliente), _jugador.Cliente);
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("Conexión finalizada", "¡Atencion!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }