Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.net.Socket;
  7. import java.util.Scanner;
  8.  
  9. public class Client implements Runnable {
  10.     private Socket socket;
  11.     private DataOutputStream dout;
  12.     private DataInputStream din;
  13.  
  14.     public Client(String host, int port) {
  15.  
  16.         try {
  17.             socket = new Socket(host, port);
  18.             System.out.println("Connected to " + socket);
  19.  
  20.             din = new DataInputStream(socket.getInputStream());
  21.             dout = new DataOutputStream(socket.getOutputStream());
  22.  
  23.             new Thread(this).start();
  24.         } catch (IOException ioe) {
  25.             System.out.println(ioe);
  26.         }
  27.     }
  28.  
  29.     private void sendCommand(String command) {
  30.         try {
  31.             dout.writeUTF(command);
  32.         } catch (IOException ioe) {
  33.             System.out.println(ioe);
  34.         }
  35.     }
  36.  
  37.     public void run() {
  38.         try {
  39.             Scanner scanner = new Scanner(System.in);
  40.             String command;
  41.             while (true) {
  42.                 System.out.print(">");
  43.                 command = scanner.nextLine();
  44.                 if ( command.toLowerCase().equals("exit") ) {
  45.                     System.out.println("Terminating...");
  46.                     scanner.close();
  47.                     System.exit(0);
  48.                 } else {
  49.                     sendCommand(command);
  50.                 }
  51.                 if ( command.equals("ls") ) {
  52.                     // Get the list of files in the Directory from the server
  53.                     int size = din.read();
  54.                     for (int i = 0; i < size; i++) {
  55.                         System.out.println(din.readUTF());
  56.                     }
  57.                 } else if ( command.substring(0, 2).equals("dl") ) {
  58.                     // Download the file from the server
  59.                     System.out.println("Downloading " + command.substring(3));
  60.                    
  61.                     long size = din.readLong();
  62.                     System.out.println("File size: " + size);
  63.                     byte[] buffer = new byte[(int) size];
  64.                     OutputStream output = new FileOutputStream(command.substring(3));
  65.                     int bytesRead = 0;
  66.                     while(size > 0 && (bytesRead = din.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1) {
  67.                         output.write(buffer, 0, bytesRead);
  68.                         size -= bytesRead;
  69.                     }
  70.                    
  71.                     output.close();
  72.                 }
  73.             }
  74.         } catch (IOException ioe) {
  75.             System.out.println(ioe);
  76.         }
  77.     }
  78.  
  79.     public static void main(String[] args) {
  80.         new Client("localhost", 8585);
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement