Advertisement
wieruowq

Client.java

Nov 1st, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.76 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. import java.nio.file.*;
  5. import static java.nio.charset.StandardCharsets.*;
  6. public class Client implements Runnable
  7. {
  8.     DataOutputStream output2 = null;
  9.     DataInputStream input = null;
  10. //  ClientConnect listenForFilePort = null;
  11.     public boolean isConnected = true;
  12.     int ConnectOnce = 0;
  13.     public String[] clientArgs;
  14.     public static Socket socket;
  15.     public Socket fileSocket;
  16.     public boolean keepRunning = true;
  17.     public String option = "";
  18.     public int differentPort;
  19.     public int ConnectOnce1 = 0;
  20.     public int normalPort = 0;
  21.     public Client(String[] args) throws IOException
  22.     {
  23.         this.clientArgs = args;
  24.         if(ConnectOnce == 0)
  25.         {
  26.             try
  27.             {
  28.                 differentPort = Integer.valueOf(clientArgs[1]);
  29.                 String host = "localhost";
  30.                 InetAddress address = InetAddress.getByName(host);
  31.                 String port_number1= clientArgs[3];
  32.                 normalPort = Integer.valueOf(port_number1);
  33.                 socket = new Socket(address, normalPort);
  34.                 ConnectOnce = 4;
  35.             }
  36.             catch (UnknownHostException e)
  37.             {
  38.                 e.printStackTrace();    
  39.             }
  40.             catch (IOException e)
  41.             {
  42.                 e.printStackTrace();
  43.             }
  44.             finally
  45.             {  
  46.             }
  47.         }
  48.     }
  49.     public void clientRun2(String[] args)
  50.     {
  51.         clientArgs = args;
  52.         clientArgs = Arrays.copyOf(args, args.length);
  53.         clientSend.start();
  54.  
  55.        
  56.         /*
  57.          * listenForFilePort = new ClientConnect(socket, differentPort);
  58.          * listenForFilePort.clientConnection(differentPort, fileSocket);
  59.          */
  60.     }
  61.     Thread clientSend = new Thread()
  62.     {
  63.         public void run()
  64.         {  
  65.             OutputOptions();
  66.             while(isConnected)
  67.             {
  68.                 try
  69.                 {
  70.                    ClientRecieve clientThread = new ClientRecieve(socket);
  71.                    clientThread.start();
  72.                    DataInputStream userKeyboardInput = new DataInputStream(System.in);
  73.                    String line2 = userKeyboardInput.readLine();
  74.                    if(line2.equals("m") || line2.equals("M"))
  75.                    {
  76.                         ClientSend();
  77.                    }
  78.                    else if(line2.equals("f") || line2.equals("F"))
  79.                    {
  80.                        FileRequest();
  81.                    }
  82.                    else if(line2.equals("x") || line2.equals("X"))
  83.                    {
  84.                        isConnected = false;
  85.                        System.exit(0);
  86.                        break;
  87.                    }
  88.                    else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
  89.                            && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
  90.                    {
  91.                        System.out.println("(invalid choice)");
  92.                    }
  93.                }
  94.                catch ( Exception e )
  95.                {
  96.                    System.out.println( e.getMessage() );
  97.                }
  98.             }
  99.         }
  100.     };
  101.     public void ClientSend()
  102.     {
  103.         try
  104.         {
  105.             DataInputStream userKeyboardInput = new DataInputStream(System.in);
  106.             DataOutputStream output = new DataOutputStream(socket.getOutputStream());  
  107.             System.out.println("Enter your message:");
  108.             String newMessage = userKeyboardInput.readLine();
  109.             if(newMessage!=null && !newMessage.isEmpty())
  110.             {
  111.                 output.writeUTF(newMessage);
  112.             }
  113.             output.flush();
  114.             StandardInput();
  115.         }
  116.         catch (IOException e)
  117.         {
  118.             e.printStackTrace();
  119.         }
  120.         finally
  121.         {    
  122.         }
  123.     }
  124.    
  125.     public void FileRequest() throws FileNotFoundException
  126.     {
  127.         try
  128.         {
  129.             ///
  130.              output2 = new DataOutputStream(socket.getOutputStream());  
  131.              input = new DataInputStream(socket.getInputStream());
  132.              System.out.println("Which file do you want?");
  133.              Scanner scanner = new Scanner(System.in);
  134.              String thisFileName = scanner.nextLine();
  135.              System.out.println("99877");
  136.              if(thisFileName!=null && !thisFileName.isEmpty())
  137.              {
  138.                  System.out.println("666332");
  139.  
  140.                  System.out.println("69683721");
  141.                  output2.writeUTF(thisFileName + "`" + "\n");
  142.                  System.out.println("1234123456666");
  143.                  output2.flush();
  144.                  System.out.println("flushed");
  145.                  long file_size= input.readLong();
  146.                  System.out.println("The file size is: " + file_size);
  147.                  if ( file_size == 0 )
  148.                  {
  149.                      System.out.println("File size is 0");
  150.                  }
  151.                  FileOutputStream file_out= new FileOutputStream( thisFileName );
  152.                  System.out.println("4445555");
  153.                  int number_read;
  154.                  byte[] buffer= new byte[1500];
  155.                  System.out.println("23234");
  156.                  while( (number_read= input.read( buffer)) != -1 )
  157.                  {
  158.                      file_out.write( buffer, 0, number_read );
  159.                      System.out.println("66333");
  160.                  }
  161.                  System.out.println("45531");
  162.                  System.out.println("File " + thisFileName  + " downloaded (" + number_read + " bytes read)");
  163.                  System.out.println("123411");
  164.                     StandardInput();
  165.                 }
  166.                
  167.                  
  168.         }
  169.         catch (IOException e)
  170.         {
  171.             e.printStackTrace();
  172.         }
  173.     }
  174.     public int recieveSizeAndPath(Socket socket)
  175.     {
  176.         int size = 0;
  177.         try
  178.         {
  179.             BufferedReader readfromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  180.             String fromServer = readfromServer.readLine();
  181.             if(fromServer!=null && !fromServer.isEmpty())
  182.             {
  183.                 if(fromServer.contains("size"))
  184.                 {
  185.                     String string = fromServer;
  186.                     String[] parts = string.split(":");
  187.                     String part1 = parts[0];
  188.                     String part2 = parts[1];
  189.                     System.out.println("Size of file is: " + part2);
  190.                     size = Integer.valueOf(part2);
  191.                     return size;
  192.                 }
  193.             }
  194.         }
  195.         catch (IOException e)
  196.         {
  197.             e.printStackTrace();
  198.         }
  199.         finally
  200.         {
  201.  
  202.         }
  203.         return size;
  204.     }
  205.     public static void receiveFile(int portNo, String fileLocation, Socket socket, int size) throws IOException
  206.     {
  207.         int bytesRead=0;
  208.         int current = 0;
  209.         FileOutputStream fileOutputStream = null;
  210.         BufferedOutputStream bufferedOutputStream = null;
  211.         try
  212.         {
  213.             byte [] byteArray  = new byte [size];
  214.             System.out.println("26");
  215.             InputStream inputStream = socket.getInputStream();
  216.             System.out.println("3453");
  217.             fileOutputStream = new FileOutputStream(fileLocation);
  218.             System.out.println("43252");
  219.             bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
  220.             System.out.println("849392");
  221.             bytesRead = inputStream.read(byteArray,0,byteArray.length);            
  222.             current = bytesRead;
  223.             do
  224.             {
  225.                 bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
  226.                 if(bytesRead >= 0) current += bytesRead;
  227.             }
  228.             while(bytesRead > -1);
  229.             bufferedOutputStream.write(byteArray, 0 , current);
  230.             bufferedOutputStream.flush();                                      
  231.             System.out.println("File " + fileLocation  + " downloaded ( size: " + current + " bytes read)");
  232.         }
  233.         catch (IOException e)
  234.         {
  235.             e.printStackTrace();
  236.         }
  237.         finally
  238.         {
  239.         }
  240.     }
  241.     Thread ClientRead = new Thread();
  242.     void OutputOptions()
  243.     {
  244.         System.out.println("Enter an option ('m', 'f', 'x'): ");
  245.         System.out.println("(M)essage (send)");
  246.         System.out.println("(F)ile (request) ");
  247.         System.out.println("e(X)it ");
  248.     }
  249.     @Override
  250.     public void run()
  251.     {
  252.     }
  253.     public void StandardInput()
  254.     {
  255.         OutputOptions();
  256.         while(true)
  257.         {
  258.             String option = "";
  259.             String newmessage = "";
  260.             try
  261.             {
  262.                 DataInputStream userKeyboardInput = new DataInputStream(System.in);
  263.                 String line2 = userKeyboardInput.readLine();
  264.                 if(line2.equals("m") || line2.equals("M"))
  265.                 {
  266.                      ClientSend();
  267.                 }
  268.                 else if(line2.equals("f") || line2.equals("F"))
  269.                 {
  270.                     FileRequest();
  271.                 }
  272.                 else if(line2.equals("x") || line2.equals("X"))
  273.                 {
  274.                    isConnected = false;
  275.                     System.exit(0);
  276.                     break;
  277.                 }
  278.                 else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
  279.                         && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
  280.                 {
  281.                     System.out.println("(invalid choice)");
  282.                 }
  283.            }
  284.            catch ( Exception e )
  285.            {
  286.                e.printStackTrace();
  287.            }
  288.             finally
  289.             {
  290.             }
  291.         }
  292.     }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement