Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SERVER
- import java.net._
- import java.io._
- import scala.io._
- import scala.util.control._
- import java.lang.String
- import collection.mutable.HashMap
- object HelloServerWorld {
- class ScalaServer(val _conn: Socket, _root:String) extends Runnable {
- val CONN = _conn
- var root:String = _root
- val in = new DataInputStream(CONN.getInputStream())
- val out = new DataOutputStream(CONN.getOutputStream())
- def getListOfFiles(dir: String):List[File] = {
- val d = new File(dir)
- if (d.exists && d.isDirectory) {
- d.listFiles.filter(_.isFile).toList
- } else {
- List[File]()
- }
- }
- def Pwd() : Unit = {
- var dir:String = '\t'+System.getProperty("user.dir")
- out.writeUTF("\n"+dir)
- out.flush()
- }
- def ls() : Unit = {
- val Currdir = System.getProperty("user.dir")
- val files = getListOfFiles(Currdir)
- var dirs :String = "\n"
- for ( i <- 0 to (files.length - 1)) {
- dirs += '\t'+files(i).getName()+'\n'
- }
- out.writeUTF(dirs)
- out.flush()
- }
- def cd() : Unit = {
- val outer = new Breaks
- outer.breakable {
- while(true)
- {
- out.writeUTF("Insert absolute path of the directory that you want to navigate to: ")
- out.flush()
- var dir :String = in.readUTF()
- var CurrentDir:String = System.getProperty("user.dir")
- var dirToBE:String = root.replace('\\','/')
- println(dir)
- System.setProperty("user.dir", dirToBE+"/"+dir)
- println(System.getProperty("user.dir"))
- out.writeUTF("Current directory is: "+System.getProperty("user.dir"))
- out.flush()
- outer.break
- }
- }
- }
- def saveFile() : Unit = {
- out.writeUTF("Input file path")
- out.flush()
- val archivoName = in.readUTF()
- val tam = in.readUTF()
- if(archivoName != "File does not exists"){
- val fos = new FileOutputStream(archivoName)
- var bytes = new Array[Byte](1024)
- var bytesLeidos:Int = 0
- var tama:Int = Integer.parseInt(tam)
- println(tama)
- while(tama > 0) {
- if(tama <= 1024){
- bytesLeidos = in.read(bytes, 0, tama)
- } else{
- bytesLeidos = in.read(bytes, 0, 1024)
- }
- tama -= bytesLeidos
- fos.write(bytes, 0, bytesLeidos)
- }
- fos.close()
- out.writeUTF("Received successfully")
- out.flush()
- }else {
- out.writeUTF("File does not exists")
- out.flush()
- }
- }
- def mkdir() : Unit = {
- out.writeUTF("Name of new directory: ")
- out.flush()
- val _dirName = in.readUTF()
- val mkDir = new File(System.getProperty("user.dir") + "/" + _dirName)
- val successful = mkDir.mkdir();
- if(successful) {
- out.writeUTF("Directory successfully created")
- }else {
- out.writeUTF("Unable to create directory")
- }
- out.flush()
- }
- def rm() : Unit = {
- out.writeUTF("Name of file/directory to delete: ")
- out.flush()
- val delPath = in.readUTF()
- val rm = new File(System.getProperty("user.dir") + "/" + delPath)
- if(rm.exists) {
- rm.delete()
- out.writeUTF("Deleted successfully")
- }else {
- out.writeUTF("Unable to delete")
- }
- out.flush()
- }
- def sendFile() : Unit = {
- out.writeUTF("File to download")
- out.flush()
- val estoQuiero = in.readUTF()
- val archivo = new File(estoQuiero)
- if(archivo.exists){
- out.writeUTF(archivo.length.toString())
- out.flush()
- val fos = out
- val leer = new FileInputStream(estoQuiero);
- var bytes = new Array[Byte](1024);
- var tama:Int = archivo.length.intValue()
- var bytesLeidos:Int = 0
- while(tama > 0) {
- if(tama <= 1024){
- bytesLeidos = leer.read(bytes, 0, tama)
- } else{
- bytesLeidos = leer.read(bytes, 0, 1024)
- }
- tama -= bytesLeidos
- fos.write(bytes, 0, bytesLeidos)
- }
- leer.close();
- out.writeUTF("File sent successfully")
- out.flush()
- }else{
- out.writeUTF("File does not exists")
- out.flush()
- }
- }
- def userHandler(user: String, password: String) : Boolean = {
- var usuarios = HashMap[String, String]()
- val archivo = new File("users.txt")
- var leido = ""
- if (archivo.exists){
- Source.fromFile("users.txt" ).foreach{case line =>
- leido += line
- }
- var splitUsers: Array[java.lang.String] = leido.split(",").map(_.trim)
- var esUsuario = 0;
- var arrayTam = splitUsers.size
- while(arrayTam > esUsuario){
- usuarios(splitUsers(esUsuario)) = splitUsers(esUsuario+1)
- esUsuario += 2
- }
- }
- if(usuarios contains user){
- if(usuarios(user) == password){
- println("Login success")
- return true
- }else{
- println("Contraseña incorrecta")
- return false
- }
- }else {
- out.writeUTF("User does not exist, do you want to create [Y/N]")
- if(in.readUTF()=="Y"){
- usuarios(user) = password
- println("Usuario creado y login success")
- }
- }
- val writer = new PrintWriter(new File("users.txt" ))
- var tam = usuarios.size
- var iteracion = 0;
- usuarios.foreach{case(key, value) =>
- writer.write(key)
- writer.write(",")
- writer.write(value)
- if(iteracion != (tam-1)){
- writer.write(",")
- }
- iteracion += 1;
- }
- writer.close()
- return false
- }
- def logic() : Unit = {
- while(true){
- var login:String = "\n"
- login+="\t"+"--------Login--------"+"\n"
- login+="\t"+"Input you username: "+"\n"
- out.writeUTF(login)
- var userName:String= in.readUTF()
- out.writeUTF("Input you password: ")
- var password:String= in.readUTF()
- System.setProperty("user.dir",root)
- if(userHandler(userName,password)){
- while(true){
- out.writeUTF(" ")
- val received = in.readUTF()
- println("Message Received:" + received)
- if(received == "pwd"){
- Pwd()
- }else if (received == "ls"){
- ls()
- }else if(received == "cd"){
- cd()
- }else if(received == "get"){
- sendFile()
- out.writeUTF("")
- }else if(received == "put"){
- saveFile()
- }else if(received == "rm"){
- rm()
- }else if(received == "mkdir"){
- mkdir()
- }else{
- out.writeUTF('"'+received+'"'+" is not a valid command.")
- out.flush()
- }
- }
- }else{
- out.writeUTF("Acces denied")
- out.flush()
- }
- }
- }
- def run {
- logic()
- }
- }
- def main(args: Array[String]){
- val server = new ServerSocket(9999)
- val cont = true
- var root:String = System.getProperty("user.dir")
- while (cont) {
- val s = server.accept()
- println(s.getRemoteSocketAddress())
- new Thread(new ScalaServer(s,root)).start
- }
- }
- }
- //CLIENT
- import java.net._
- import java.io._
- import scala.io._
- import scala.util.control._
- def sendFile() : Unit = {
- val mensaje2 = in.readUTF()
- println("Received: " + mensaje2)
- val text2 = StdIn.readLine()
- val archivo = new File(text2)
- if(archivo.exists){
- out.writeUTF(archivo.getName())
- out.flush()
- out.writeUTF(archivo.length.toString())
- out.flush()
- val leer = new FileInputStream(text2);
- var bytes = new Array[Byte](1024);
- var tama:Int = archivo.length.intValue()
- var bytesLeidos:Int = 0
- while(tama > 0) {
- if(tama <= 1024){
- bytesLeidos = leer.read(bytes, 0, tama)
- } else{
- bytesLeidos = leer.read(bytes, 0, 1024)
- }
- tama -= bytesLeidos
- out.write(bytes, 0, bytesLeidos)
- }
- leer.close();
- }else{
- out.writeUTF("No existe")
- out.flush()
- out.writeUTF(0+"")
- out.flush()
- }
- }
- def getFile() : Unit = {
- val mensaje2 = in.readUTF()
- println("Received: " + mensaje2)
- val estoQuiero = StdIn.readLine()
- out.writeUTF(estoQuiero)
- out.flush()
- val mensaje3 = in.readUTF()
- if(mensaje3 != "File does not exists"){
- println("receiving file")
- //val tam = Integer.parseInt(mensaje3)
- val fos = new FileOutputStream(estoQuiero)
- var bytes = new Array[Byte](1024)
- var bytesLeidos:Int = 0
- var tama:Int = Integer.parseInt(mensaje3)
- while(tama > 0) {
- if(tama <= 1024){
- bytesLeidos = in.read(bytes, 0, tama)
- } else{
- bytesLeidos = in.read(bytes, 0, 1024)
- }
- tama -= bytesLeidos
- fos.write(bytes, 0, bytesLeidos)
- }
- fos.close()
- val mensaje4 = in.readUTF()
- println("Received: " + mensaje4)
- }else{
- println("Received: " + mensaje3)
- }
- }
- val s = new Socket(InetAddress.getByName("localhost"), 9999)
- lazy val in = new DataInputStream(s.getInputStream())
- val out = new DataOutputStream(s.getOutputStream())
- while (true) {
- println("Message from server: " + in.readUTF())
- println("Text to send: ")
- val text = StdIn.readLine()
- out.writeUTF(text)
- out.flush()
- if(text=="put")
- {
- sendFile()
- }else if(text == "get")
- {
- getFile()
- }
- //s.close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement