Пример #1
0
        private static void UploadPicture(Socket socket)
        {
            Console.WriteLine("Por favor inserte el path de la imagen que desea subir");
            string path = Console.ReadLine();

            while (path.Equals(string.Empty) || !FileHandler.FileExists(path))
            {
                Console.WriteLine("path invalido");
                path = Console.ReadLine();
            }

            Console.WriteLine("Ingrese el nombre que desea darle a la foto");
            string         name    = Console.ReadLine();
            CommandPackage package = new CommandPackage(HeaderConstants.Request, CommandConstants.SendPicture, name);

            CommandProtocol.SendCommand(socket, package);

            FileProtocol fileProtocol = new FileProtocol(socket);

            fileProtocol.SendFile(path);
            Console.WriteLine("Su foto fue subida con exito");
        }
Пример #2
0
        static void Main(string[] args)
        {
            bool isRunning = true;

            var clientEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);

            var serverEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(clientEndpoint);

            socket.Connect(serverEndpoint);

            Console.WriteLine("Conectado al servidor!");

            while (isRunning)
            {
                Message authMessage = new Message(HeaderConstants.Request, CommandConstants.UserLogged);
                MessageProtocol.SendMessage(socket, authMessage);
                Message authMessageResponse = MessageProtocol.ReceiveMessage(socket);

                if (authMessageResponse.Header.ICommand == CommandConstants.UserLogged)
                {
                    showAuthMenu();
                    var opcion = Console.ReadLine();
                    switch (opcion)
                    {
                    case "users":
                        Message loginMessage = new Message(HeaderConstants.Request, CommandConstants.ListUsers);
                        MessageProtocol.SendMessage(socket, loginMessage);
                        Message messageResponse = MessageProtocol.ReceiveMessage(socket);
                        Console.WriteLine(messageResponse.MessageText);
                        break;

                    case "upload":
                        string userpath = userForm("Ingrese direccion del archivo");
                        while (!Handler.FileExists(userpath))
                        {
                            userpath = userForm("Ingrese una direccion correcta del archivo");
                        }
                        Message userFileMessage = new Message(HeaderConstants.Request, CommandConstants.UploadFile, userpath);
                        MessageProtocol.SendMessage(socket, userFileMessage);

                        FileProtocol fp = new FileProtocol(socket);
                        fp.SendFile(userpath);
                        break;

                    case "userP":
                        string  userName    = userForm("Ingrese nombre del usuario");
                        Message userMessage = new Message(HeaderConstants.Request, CommandConstants.ListUserPhotos, userName);
                        MessageProtocol.SendMessage(socket, userMessage);
                        Message userPhotosResponse = MessageProtocol.ReceiveMessage(socket);
                        Console.WriteLine(userPhotosResponse.MessageText);
                        break;

                    case "logout":
                        Message logoutMessage = new Message(HeaderConstants.Request, CommandConstants.UserNotLogged);
                        MessageProtocol.SendMessage(socket, logoutMessage);
                        break;

                    case "photoC":
                        string  photoName    = userForm("Ingrese nombre de la foto");
                        Message photoMessage = new Message(HeaderConstants.Request, CommandConstants.CommentPhoto, photoName);
                        MessageProtocol.SendMessage(socket, photoMessage);
                        Message photoResponse = MessageProtocol.ReceiveMessage(socket);
                        Console.WriteLine(photoResponse.MessageText);
                        break;

                    case "exit":
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();
                        isRunning = false;
                        break;

                    default:
                        Console.WriteLine("Opcion invalida");
                        break;
                    }
                }
                else
                {
                    showClientMenu();
                    var opcion = Console.ReadLine();

                    switch (opcion)
                    {
                    case "login":
                        string  userLogin    = userForm("Ingrese Nombre de usuario");
                        Message loginMessage = new Message(HeaderConstants.Request, CommandConstants.Login, userLogin);
                        MessageProtocol.SendMessage(socket, loginMessage);
                        break;

                    case "register":
                        string  userRegister = userForm("Ingrese Nombre de usuario");
                        Message userMessage  = new Message(HeaderConstants.Request, CommandConstants.Register, userRegister);
                        MessageProtocol.SendMessage(socket, userMessage);
                        Message messageResponse = MessageProtocol.ReceiveMessage(socket);
                        Console.WriteLine(messageResponse.MessageText);
                        break;

                    case "exit":
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Close();
                        isRunning = false;
                        break;

                    default:
                        Console.WriteLine("Opcion invalida");
                        break;
                    }
                }
            }

            Console.WriteLine("Cerrando Applicación");
        }