Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package testechoserver;
  7.  
  8. /**
  9.  *
  10.  * @author bumblebee
  11.  */
  12. import java.net.*;
  13. import java.io.*;
  14.  
  15. public class TestEchoServer {
  16.  
  17.     private static final String HEADER = "STATUS 200 OK \r\n";
  18.     private static final String ERROR = "STATUS 400 BAD RESPONSE";
  19.  
  20.     public static void main(String[] args) throws IOException {
  21.  
  22.         int portNumber = 8000;
  23.  
  24.         try (
  25.                 ServerSocket serverSocket = new ServerSocket(portNumber);
  26.                 Socket clientSocket = serverSocket.accept();
  27.                 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  28.                 OutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
  29.                 OutputStream auxOut = clientSocket.getOutputStream();) {
  30.             String inputLine;
  31.             while ((inputLine = in.readLine()) != null) {
  32.                 //out.println(inputLine);
  33.                 String[] sArray = inputLine.split(" ");
  34.                 System.out.println(sArray[0]);
  35. //                System.out.println(sArray[1]);
  36.                 if (sArray[0].toLowerCase().equals("pwd")) {
  37.                     String workdir = System.getProperty("user.dir");
  38.                     if (sArray.length > 1) {
  39.                         if (sArray[1].toLowerCase().equals("crlf")) {
  40.                             workdir = HEADER + "LINES: 4 \r\n" + "\r\n" + "WORKDIR:" + workdir + "\r\n";
  41.                         }
  42.                     }
  43.                     byte[] b = workdir.getBytes();
  44.                     auxOut.write(b);
  45.                     System.out.println(workdir);
  46.                     auxOut.flush();
  47.                 } else if (sArray[0].toLowerCase().equals("ls")) {
  48.  
  49.                     String workdir = System.getProperty("user.dir");
  50.                     File folder = new File(workdir);
  51.                     File[] files = folder.listFiles();
  52.                     String auxStr = "";
  53.                     for (int i = 0; i < files.length; i++) {
  54.                         System.out.println(files[i].getName());
  55.                         String tmpStr = files[i].getName();
  56.                         auxStr = auxStr + tmpStr + " \r\n";
  57.  
  58.                     }
  59.                     if (sArray.length > 1) {
  60.                         if (sArray[1].toLowerCase().equals("crlf")) {
  61.                             int lines = 3 + files.length;
  62.                             auxStr = HEADER + "LINES : " + lines + "\r\n" + "\r\n" + "FILES: " + auxStr;
  63.                         }
  64.                     }
  65.                     byte[] b = auxStr.getBytes();
  66.                     System.out.println(b.length + " hej");
  67.                     auxOut.write(b);
  68.                     auxOut.flush();
  69.  
  70.                 } else if (sArray[0].toLowerCase().equals("dl")) {
  71.                     String workdir = System.getProperty("user.dir");
  72.                     System.out.println(workdir);
  73.                     File folder = new File(workdir);
  74.                     File[] files = folder.listFiles();
  75.  
  76.                     for (int i = 0; i < files.length; i++) {
  77.                         if (sArray.length > 1) {
  78.                             if (sArray[1].equals(files[i].getName())) {
  79.                                 System.out.println("correct filename");
  80.                                 int count;
  81.                                 byte[] buffer = new byte[1024];
  82.                                 System.out.println("connected");
  83.                                 OutputStream tmpout = clientSocket.getOutputStream();
  84.                                 BufferedInputStream tmpin = new BufferedInputStream(new FileInputStream(files[i]));
  85.                                 while ((count = tmpin.read(buffer)) > 0) {
  86.                                     System.out.println("downloading....");
  87.                                     tmpout.write(buffer, 0, count);
  88.                                     tmpout.flush();
  89.                                 }
  90.                                 System.out.println("download complete");
  91.                                 tmpout.close();
  92.                                 tmpin.close();
  93.  
  94.                             }
  95.                         } else {
  96.                             byte[] b = ERROR.getBytes();
  97.                             auxOut.write(b);
  98.                             auxOut.flush();
  99.                            
  100.                         }
  101.                     }
  102.  
  103.                 } else if (!sArray[0].toLowerCase().equals("ls") || !sArray[0].toLowerCase().equals("pwd") || !sArray[0].toLowerCase().equals("dl")) {
  104.                     byte[] b = ERROR.getBytes();
  105.                     auxOut.write(b);
  106.                     auxOut.flush();
  107.                 }
  108.             }
  109.         } catch (IOException e) {
  110.             System.out.println("Exception caught when trying to listen on port "
  111.                     + portNumber + " or listening for a connection");
  112.             System.out.println(e.getMessage());
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement