Advertisement
Guest User

Untitled

a guest
May 7th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package ftp
  2.  
  3. import ftp.messages.FileMessage
  4. import ftp.messages.LoginMessage
  5. import ftp.messages.Message
  6. import ftp.messages.TextMessage
  7. import java.io.BufferedReader
  8. import java.io.File
  9. import java.io.PrintWriter
  10. import java.net.Socket
  11. import java.nio.file.Files
  12. import java.nio.file.Paths
  13. import kotlin.concurrent.thread
  14.  
  15. /**
  16. * Created by Florian Hofmair on 3/14/17.
  17. */
  18.  
  19. class FTPClient {
  20. lateinit var client: Client
  21.  
  22. fun connect(ip: String, port: Int) {
  23. val c = Socket(ip, port)
  24. client = Client(c)
  25.  
  26. thread {
  27. while (!client.conn.isClosed) {
  28. val msg = client.input.readObject() as Message
  29.  
  30. when(msg) {
  31. is TextMessage -> println(msg.text)
  32. is FileMessage -> println(msg.path + "\n" + msg.content)
  33. is LoginMessage -> println(msg.user + ":" + msg.pass)
  34. }
  35. }
  36. }
  37. }
  38.  
  39. fun disconnect() {
  40. client.conn.close()
  41. client.output.close()
  42. client.input.close()
  43. }
  44. }
  45.  
  46. fun main(args: Array<String>) {
  47. val client = FTPClient()
  48. client.connect("192.168.101.75", 6666)
  49.  
  50. println("Username: ")
  51. val user = readLine()
  52. println("Password: ")
  53. val pass = readLine()
  54.  
  55. client.client.sendLogin(user!!, pass!!)
  56.  
  57. loop@ while(true) {
  58. val line = readLine()
  59. if(line != null) {
  60. when(line) {
  61. "quit" -> break@loop
  62. else -> {
  63. if(line.startsWith("put")) {
  64. val a = line.split(' ').drop(1)
  65. if(a.size < 2)
  66. continue@loop
  67.  
  68. if(Files.exists(Paths.get(a[1]))) {
  69. client.client.sendFile(File(a[1]).readText(), a[2])
  70. println("Uploading ${a[1]} to ${a[2]}")
  71. } else
  72. println("File ${a[1]} not found")
  73. }
  74. else {
  75. client.client.sendMessage(line)
  76. }
  77. }
  78. }
  79. }
  80. }
  81. client.disconnect()
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement