Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package testechoserver;
- /**
- *
- * @author bumblebee
- */
- import java.net.*;
- import java.io.*;
- public class TestEchoServer {
- private static final String HEADER = "STATUS 200 OK \r\n";
- private static final String ERROR = "STATUS 400 BAD RESPONSE";
- public static void main(String[] args) throws IOException {
- int portNumber = 8000;
- try (
- ServerSocket serverSocket = new ServerSocket(portNumber);
- Socket clientSocket = serverSocket.accept();
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- OutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
- OutputStream auxOut = clientSocket.getOutputStream();) {
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- //out.println(inputLine);
- String[] sArray = inputLine.split(" ");
- System.out.println(sArray[0]);
- // System.out.println(sArray[1]);
- if (sArray[0].toLowerCase().equals("pwd")) {
- String workdir = System.getProperty("user.dir");
- if (sArray.length > 1) {
- if (sArray[1].toLowerCase().equals("crlf")) {
- workdir = HEADER + "LINES: 4 \r\n" + "\r\n" + "WORKDIR:" + workdir + "\r\n";
- }
- }
- byte[] b = workdir.getBytes();
- auxOut.write(b);
- System.out.println(workdir);
- auxOut.flush();
- } else if (sArray[0].toLowerCase().equals("ls")) {
- String workdir = System.getProperty("user.dir");
- File folder = new File(workdir);
- File[] files = folder.listFiles();
- String auxStr = "";
- for (int i = 0; i < files.length; i++) {
- System.out.println(files[i].getName());
- String tmpStr = files[i].getName();
- auxStr = auxStr + tmpStr + " \r\n";
- }
- if (sArray.length > 1) {
- if (sArray[1].toLowerCase().equals("crlf")) {
- int lines = 3 + files.length;
- auxStr = HEADER + "LINES : " + lines + "\r\n" + "\r\n" + "FILES: " + auxStr;
- }
- }
- byte[] b = auxStr.getBytes();
- System.out.println(b.length + " hej");
- auxOut.write(b);
- auxOut.flush();
- } else if (sArray[0].toLowerCase().equals("dl")) {
- String workdir = System.getProperty("user.dir");
- System.out.println(workdir);
- File folder = new File(workdir);
- File[] files = folder.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (sArray.length > 1) {
- if (sArray[1].equals(files[i].getName())) {
- System.out.println("correct filename");
- int count;
- byte[] buffer = new byte[1024];
- System.out.println("connected");
- OutputStream tmpout = clientSocket.getOutputStream();
- BufferedInputStream tmpin = new BufferedInputStream(new FileInputStream(files[i]));
- while ((count = tmpin.read(buffer)) > 0) {
- System.out.println("downloading....");
- tmpout.write(buffer, 0, count);
- tmpout.flush();
- }
- System.out.println("download complete");
- tmpout.close();
- tmpin.close();
- }
- } else {
- byte[] b = ERROR.getBytes();
- auxOut.write(b);
- auxOut.flush();
- }
- }
- } else if (!sArray[0].toLowerCase().equals("ls") || !sArray[0].toLowerCase().equals("pwd") || !sArray[0].toLowerCase().equals("dl")) {
- byte[] b = ERROR.getBytes();
- auxOut.write(b);
- auxOut.flush();
- }
- }
- } catch (IOException e) {
- System.out.println("Exception caught when trying to listen on port "
- + portNumber + " or listening for a connection");
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement