Пример #1
0
        static void Main(string[] args)
        {
            if (!File.Exists("portas.txt"))
            {
                var stream = File.Create("portas.txt");
                stream.Close();
                StreamWriter arq = new StreamWriter("portas.txt");
                arq.WriteLine("1500");
                arq.Close();
            }

            StreamReader file = new StreamReader("portas.txt");

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            porta = int.Parse(file.ReadLine());

            IPAddress  server = IPAddress.Parse("127.0.0.1");
            IPEndPoint ep     = new IPEndPoint(server, porta);

            String  data;
            int     comand;
            int     chave;
            Comando comando;

            byte[] sendbuf = new byte[1450];

            Task threadReceive;

            while (true)
            {
                Console.WriteLine("Qual comando(ADD-1/READ-2/UPDATE-3/DELETE-4/LISTAR-5/DESLIGAR-6/SNAPSHOT-8)");
                if (!int.TryParse(Console.ReadLine(), out comand) || comand <= 0 || comand > 8)
                {
                    Console.WriteLine("Comando Inválido!");
                    continue;
                }

                switch (comand)
                {
                case (int)Comandos.ADD:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }

                    Console.WriteLine("Insira dado");
                    data = Console.ReadLine();

                    comando = new Comando(Comandos.ADD, chave, data);
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    if (sendbuf.Length > 1450)
                    {
                        Console.WriteLine("Tamanho de string inválida!");
                        continue;
                    }
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.READ:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }

                    comando = new Comando(Comandos.READ, chave, "a");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.UPDATE:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }
                    Console.WriteLine("Insira dado");
                    data    = Console.ReadLine();
                    comando = new Comando(Comandos.UPDATE, chave, data);
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    if (sendbuf.Length > 1450)
                    {
                        Console.WriteLine("Tamanho de string inválida!");
                        continue;
                    }
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.DELETE:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }
                    comando = new Comando(Comandos.DELETE, chave, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.DESLIGAR:
                    comando = new Comando(Comandos.DESLIGAR, 0, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.LISTAR:
                    comando = new Comando(Comandos.LISTAR, 0, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.SNAPSHOT:
                    Console.WriteLine("Insira o tempo(segundos): ");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Tempo inválido");
                        continue;
                    }
                    comando = new Comando(Comandos.SNAPSHOT, chave, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;
                }
                lock (threadBlock)
                {
                    if (!threadOn)
                    {
                        threadOn      = true;
                        threadReceive = new Task(() => ThreadReceive(s));
                        threadReceive.Start();
                    }
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            const string ip   = "127.0.0.1";
            const int    port = 8082;

            #region TCP

            //var tcpEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
            //var tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //Console.WriteLine("Write down your message:");
            //var message = Console.ReadLine();

            //var data = Encoding.UTF8.GetBytes(message);

            //tcpSocket.Connect(tcpEndPoint);
            //tcpSocket.Send(data);

            //var buffer = new byte[256];
            //var size = 0;
            //var answer = new StringBuilder();

            //do
            //{
            //    size = tcpSocket.Receive(buffer);
            //    answer.Append(Encoding.UTF8.GetString(buffer, 0, size));
            //}
            //while (tcpSocket.Available > 0);

            //Console.WriteLine(answer.ToString());

            //tcpSocket.Shutdown(SocketShutdown.Both);
            //tcpSocket.Close();

            //Console.ReadKey();
            #endregion

            #region UDP
            var udpEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
            var udpSocket   = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSocket.Bind(udpEndPoint);

            while (true)
            {
                Console.WriteLine("Write your message: ");
                var message = Console.ReadLine();

                EndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8081);
                udpSocket.SendTo(Encoding.UTF8.GetBytes(message), serverEndPoint);

                var buffer = new byte[256];
                var size   = 0;
                var data   = new StringBuilder();

                do
                {
                    size = udpSocket.ReceiveFrom(buffer, ref serverEndPoint);
                    data.Append(Encoding.UTF8.GetString(buffer));
                }while (udpSocket.Available > 0);

                Console.WriteLine(data);
                //Console.ReadLine();
            }

            #endregion
        }