Beispiel #1
0
        public static void Union()
        {
            // Incoming data from the client.
            string data = null;

            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the
            // host running the application.
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = IPAddress.Parse("10.9.17.190");
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 5000);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily,
                                         SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and
            // listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();
                    data = null;

                    // An incoming connection needs to be processed.
                    string[] envio        = new string[10000];
                    string[] acceso       = new string[10000];
                    string[] carrera      = new string[100];
                    byte[]   recibir_info = new byte[10000];
                    byte[]   msg          = new byte[10000];
                    int      array_size   = 0;
                    int      poscoma      = 0;

                    array_size = handler.Receive(recibir_info, 0, recibir_info.Length, 0);//recibir
                    Array.Resize(ref recibir_info, array_size);
                    data = Encoding.Default.GetString(recibir_info);

                    string        opcion = data.Substring(0, 1);//aqui cojo la opcion ya sea LDAP,CREAR VOTACION,Ver Votacion,Votar
                    SqlConnection cn     = new SqlConnection();
                    cn = new SqlConnection("Data Source=localhost;Initial Catalog=VotUcaWebApi;Integrated Security=True");
                    cn.Open();

                    switch (opcion)
                    {
                    case "1":    //LDAP
                        poscoma = data.IndexOf(",");
                        string usuario    = Descrifado.Login(data.Substring(1, poscoma - 1));
                        string contraseña = Descrifado.Login(data.Substring(poscoma + 1));
                        acceso[0] = Credenciales(usuario, contraseña);    //credenciales tiene la conexion con la LDAP
                        msg       = Encoding.ASCII.GetBytes(acceso[0] + ",");
                        break;

                    case "2":    //CREAR VOTACION
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 9)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            Console.WriteLine(envio[i]);        //imprimo los datos de la votacion
                            data = data.Substring(poscoma + 1);
                            i++;
                        }
                        acceso = Insertar(1, envio[1], envio[2], envio[3], envio[4], envio[5], envio[0], envio[6], envio[7], envio[8]);
                    }
                    break;

                    case "3":    //VER VOTACION
                    {
                        DateTime localDate  = DateTime.Today;
                        DateTime localDate1 = DateTime.Now;

                        byte[] resultado = new Byte[1000];
                        data = data.Substring(1);
                        SqlCommand    consulta = new SqlCommand("Select Carrera From Usuarios where IdUca='" + data + "'", cn);
                        SqlDataReader dr       = consulta.ExecuteReader();
                        int           z        = 0;
                        while (dr.Read())
                        {
                            carrera[z] = Convert.ToString(dr[z]);
                        }
                        cn.Close();
                        cn.Open();

                        string suma = null;

                        acceso = Insertar(2, null, null, null, null, null, null, null, null, null);
                        int i = 0;
                        int a = int.Parse(acceso[999]);
                        a = a / 10;

                        int cont = 0;


                        while (a >= 0)
                        {
                            int      j     = 0;
                            double[] horas = new double[3];
                            if ((acceso[i + 7] == carrera[0]) || (acceso[i + 7] == "TODOS LOS GRADOS"))
                            {
                                while (j < 3)
                                {
                                    horas[j]      = Double.Parse(acceso[i + 9].Substring(0, 2));
                                    acceso[i + 9] = acceso[i + 9].Substring(2 + 1);
                                    j++;
                                }
                                if (DateTime.Compare(Convert.ToDateTime(acceso[i + 4]), localDate) <= 0 && DateTime.Compare(Convert.ToDateTime(acceso[i + 5]).AddHours(horas[0]).AddMinutes(horas[1]).AddSeconds(horas[2]), localDate1) >= 0)
                                {
                                    cont++;
                                    suma += acceso[i] + "," + acceso[i + 1] + "," +
                                            acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                            acceso[i + 6] + "," + acceso[i + 7] + "," + acceso[i + 8] + "," + acceso[i + 9] + ",";
                                    // }
                                }
                            }
                            i += 10;

                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "4":        //votar
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 4)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }
                        Console.WriteLine("VOTAR - Los datos han llegado al backend: " + envio[0] + "," + envio[1] + "," + envio[2] + "," + envio[3]);

                        acceso = Insertar(3, envio[0], envio[1], envio[2], envio[3], null, null, null, null, null);
                        //envio[0]=id_votaciones,envio[1]=entero del participante,envio[2] es el IdUsuario, envio[3] es el Rol del usuario
                    }
                        ; break;

                    case "5":    //votaciones futuras
                    {
                        DateTime localDate = DateTime.Today;

                        byte[] resultado = new Byte[1000];
                        data = data.Substring(1);

                        SqlCommand    consulta = new SqlCommand("Select Carrera From Usuarios where IdUca='" + data + "'", cn);
                        SqlDataReader dr       = consulta.ExecuteReader();
                        int           z        = 0;
                        while (dr.Read())
                        {
                            carrera[z] = Convert.ToString(dr[z]);
                        }
                        cn.Close();
                        cn.Open();

                        string suma = null;

                        acceso = Insertar(2, null, null, null, null, null, null, null, null, null);
                        int cont = 0;
                        int i    = 0;
                        int a    = int.Parse(acceso[999]);
                        a = a / 10;

                        while (a >= 0)
                        {
                            if (acceso[i + 7] == carrera[0] || (acceso[i + 7] == "TODOS LOS GRADOS"))
                            {
                                if (DateTime.Compare(Convert.ToDateTime(acceso[i + 4]), localDate) > 0)
                                {
                                    cont++;
                                    suma += acceso[i] + "," + acceso[i + 1] + "," +
                                            acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                            acceso[i + 6] + "," + acceso[i + 7] + "," + acceso[i + 8] + "," + acceso[i + 9] + ",";
                                }
                            }
                            i += 10;

                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "6":    //votaciones acabadas
                    {
                        DateTime localDate  = DateTime.Today;
                        DateTime localDate1 = DateTime.Now;

                        byte[] resultado = new Byte[1000];
                        data = data.Substring(1);
                        SqlCommand    consulta = new SqlCommand("Select Carrera From Usuarios where IdUca='" + data + "'", cn);
                        SqlDataReader dr       = consulta.ExecuteReader();
                        int           z        = 0;
                        while (dr.Read())
                        {
                            carrera[z] = Convert.ToString(dr[z]);
                        }
                        cn.Close();
                        cn.Open();

                        string suma = null;
                        acceso = Insertar(2, null, null, null, null, null, null, null, null, null);
                        int i = 0;
                        int a = int.Parse(acceso[999]);
                        a = a / 10;
                        int cont = 0;
                        while (a >= 0)
                        {
                            int      j     = 0;
                            double[] horas = new double[3];
                            if (acceso[i + 7] == carrera[0] || (acceso[i + 7] == "TODOS LOS GRADOS"))
                            {
                                while (j < 3)
                                {
                                    horas[j]      = Double.Parse(acceso[i + 9].Substring(0, 2));
                                    acceso[i + 9] = acceso[i + 9].Substring(2 + 1);
                                    j++;
                                }
                                if (DateTime.Compare(Convert.ToDateTime(acceso[i + 5]), localDate) <= 0 && DateTime.Compare(localDate1, Convert.ToDateTime(acceso[i + 5]).AddHours(horas[0]).AddMinutes(horas[1]).AddSeconds(horas[2])) > 0)
                                {
                                    cont++;
                                    suma += acceso[i] + "," + acceso[i + 1] + "," +
                                            acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                            acceso[i + 6] + "," + acceso[i + 7] + "," + acceso[i + 8] + "," + acceso[i + 9] + ",";
                                }
                            }
                            i += 10;
                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "7":      //Comprobar si el usuario ya ha votado.
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 2)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }
                        Console.WriteLine("Comprobacion: " + envio[0] + "," + envio[1]);
                        Console.WriteLine("Comprobando si el usuario ha votado...");
                        acceso = Insertar(4, envio[0], envio[1], null, null, null, null, null, null, null);        //0 es idvotacion,y 1 es iduca
                        msg    = Encoding.ASCII.GetBytes(acceso[0] + ",");
                        //Console.WriteLine(Encoding.Default.GetString(msg));
                    }
                    break;

                    case "8":    //ver resultados
                    {
                        byte[] resultado = new Byte[1000];
                        string suma;
                        data     = data.Substring(1);
                        envio[0] = data.Substring(0, data.IndexOf(","));
                        acceso   = Insertar(8, envio[0], null, null, null, null, null, null, null, null);

                        suma = acceso[1] + "." + acceso[2] + "." + acceso[3] + ".";

                        msg = Encoding.ASCII.GetBytes(suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "9":     //Guardar Usuario
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 3)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(9, envio[0], envio[1], envio[2], null, null, null, null, null, null);
                        Console.WriteLine("Registrando email, curso...");
                    }
                    break;

                    case "0":     //Editar una votación
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 2)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(11, envio[0], envio[1], null, null, null, null, null, null, null);
                    }
                    break;

                    case "a":     //Eliminar una votación
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 1)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(12, envio[0], null, null, null, null, null, null, null, null);
                    }
                    break;

                    case "b":     //Obtener rol usuario actual
                    {
                        string suma;
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 1)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }
                        //Console.WriteLine("Hasta aquí llego: " + envio[0]);
                        acceso = Insertar(13, envio[0], null, null, null, null, null, null, null, null);
                        suma   = acceso[0] + ",";
                        //Console.WriteLine(suma);
                        msg = Encoding.ASCII.GetBytes(suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }
                    break;
                    }
                    handler.Send(msg);//enviar
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Alerta (error en el backend): " + e.Message);
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();
        }
Beispiel #2
0
        public static void Union()
        {
            // Incoming data from the client.
            string data = null;

            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the
            // host running the application.
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = IPAddress.Parse("192.168.1.40");
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 5000);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily,
                                         SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and
            // listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();
                    data = null;

                    // An incoming connection needs to be processed.
                    string[] envio        = new string[10000];
                    string[] acceso       = new string[10000];
                    byte[]   recibir_info = new byte[10000];
                    byte[]   msg          = new byte[10000];
                    int      array_size   = 0;
                    int      poscoma      = 0;

                    array_size = handler.Receive(recibir_info, 0, recibir_info.Length, 0);//recibir
                    Array.Resize(ref recibir_info, array_size);
                    data = Encoding.Default.GetString(recibir_info);

                    string opcion = data.Substring(0, 1);//aqui cojo la opcion ya sea LDAP,CREAR VOTACION,Ver Votacion,Votar

                    switch (opcion)
                    {
                    case "1":    //LDAP
                        poscoma = data.IndexOf(",");
                        string usuario    = Descrifado.Login(data.Substring(1, poscoma - 1));
                        string contraseña = Descrifado.Login(data.Substring(poscoma + 1));
                        acceso[0] = Credenciales(usuario, contraseña);    //credenciales tiene la conexion con la LDAP
                        msg       = Encoding.ASCII.GetBytes(acceso[0] + ",");
                        break;

                    case "2":    //CREAR VOTACION
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 8)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            Console.WriteLine(envio[i]);        //imprimo los datos de la votacion
                            data = data.Substring(poscoma + 1);
                            i++;
                        }
                        acceso = Insertar(1, envio[1], envio[2], envio[3], envio[4], envio[5], envio[0], envio[6], envio[7]);
                    }
                    break;

                    case "3":    //VER VOTACION
                    {
                        DateTime localDate = DateTime.Today;

                        byte[] resultado = new Byte[1000];
                        string suma      = null;

                        acceso = Insertar(2, null, null, null, null, null, null, null, null);
                        int i = 0;
                        int a = int.Parse(acceso[9]);
                        a = a / 10;

                        int cont = 0;
                        while (a >= 0)
                        {
                            if (DateTime.Compare(Convert.ToDateTime(acceso[i + 4]), localDate) <= 0 && DateTime.Compare(Convert.ToDateTime(acceso[i + 5]), localDate) >= 0)
                            {
                                cont++;
                                suma += acceso[i] + "," + acceso[i + 1] + "," +
                                        acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                        acceso[i + 6] + ",";
                            }
                            i += 10;

                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "4":
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 3)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }
                        Console.WriteLine(envio[0] + "," + envio[1] + "," + envio[2]);

                        acceso = Insertar(3, envio[0], envio[1], envio[2], null, null, null, null, null);
                        //envio[0]=id_votaciones,envio[1]=entero del participante,envio[2] es el IdUsuario
                    }
                        ; break;

                    case "5":    //votaciones futuras
                    {
                        DateTime localDate = DateTime.Today;

                        byte[] resultado = new Byte[1000];
                        string suma      = null;

                        acceso = Insertar(2, null, null, null, null, null, null, null, null);
                        int cont = 0;
                        int i    = 0;
                        int a    = int.Parse(acceso[9]);
                        a = a / 10;

                        while (a >= 0)
                        {
                            if (DateTime.Compare(Convert.ToDateTime(acceso[i + 4]), localDate) > 0)
                            {
                                cont++;
                                suma += acceso[i] + "," + acceso[i + 1] + "," +
                                        acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                        acceso[i + 6] + ",";

                                Console.WriteLine(cont);
                            }
                            i += 10;

                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "6":    //votaciones acabadas
                    {
                        DateTime localDate = DateTime.Today;

                        byte[] resultado = new Byte[1000];
                        string suma      = null;
                        acceso = Insertar(2, null, null, null, null, null, null, null, null);
                        int i = 0;
                        int a = int.Parse(acceso[9]);
                        a = a / 10;
                        int cont = 0;
                        while (a >= 0)
                        {
                            if (DateTime.Compare(Convert.ToDateTime(acceso[i + 5]), localDate) < 0)
                            {
                                cont++;
                                suma += acceso[i] + "," + acceso[i + 1] + "," +
                                        acceso[i + 2] + "," + acceso[i + 3] + "," + acceso[i + 4] + "," + acceso[i + 5] + "," +
                                        acceso[i + 6] + "," + acceso[i + 7] + "," + acceso[i + 8] + ",";
                            }
                            i += 10;
                            a--;
                        }
                        msg = Encoding.ASCII.GetBytes(cont.ToString() + "," + suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "7":      //Comprobar si el usuario ya ha votado.
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 2)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }
                        Console.WriteLine(envio[0] + "," + envio[1]);
                        Console.WriteLine("Comprobando si el usuario ha votado...");
                        acceso = Insertar(4, envio[0], envio[1], null, null, null, null, null, null);        //0 es idvotacion,y 1 es iduca
                        msg    = Encoding.ASCII.GetBytes(acceso[0] + ",");
                        //Console.WriteLine(Encoding.Default.GetString(msg));
                    }
                    break;

                    case "8":    //ver resultados
                    {
                        byte[] resultado = new Byte[1000];
                        string suma;
                        data     = data.Substring(1);
                        envio[0] = data.Substring(0, data.IndexOf(","));
                        acceso   = Insertar(8, envio[0], null, null, null, null, null, null, null);

                        suma = acceso[1] + "," + acceso[2] + "," + acceso[3] + ",";

                        msg = Encoding.ASCII.GetBytes(suma);
                        Console.WriteLine(Encoding.Default.GetString(msg));
                    }; break;

                    case "9":     //Guardar Usuario
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 3)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(9, envio[0], envio[1], envio[2], null, null, null, null, null);
                        Console.WriteLine("Registrando email, curso...");
                    }
                    break;

                    case "0":     //Editar una votación
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 2)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(11, envio[0], envio[1], null, null, null, null, null, null);
                    }
                    break;

                    case "a":
                    {
                        data = data.Substring(1);
                        int i = 0;
                        while (i < 1)
                        {
                            poscoma  = data.IndexOf(",");
                            envio[i] = data.Substring(0, poscoma);
                            data     = data.Substring(poscoma + 1);
                            i++;
                        }

                        acceso = Insertar(12, envio[0], null, null, null, null, null, null, null);
                    }
                    break;
                    }
                    handler.Send(msg);//enviar
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Alerta (error en el backend): " + e.Message);
            }

            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();
        }