wieruowq

ServerRecieve.java

Nov 1st, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. import java.net.*;
  2. import java.nio.file.Path;
  3. import java.nio.file.Paths;
  4. import java.io.*;
  5. public class ServerRecieve extends Thread
  6. {
  7.     Server server1;
  8.     String[] args;
  9.     public int newSize = 0;
  10.     public long fileSize = 0;
  11.     int fileSize2 = 0;
  12.     int filePortNumber = 0;
  13.     Socket servSocket;
  14.     Socket fileSocket = new Socket();
  15.     boolean m_bRunThread = true;
  16.     boolean ServerOn = true;
  17.     int gotPortNumber = 0;
  18.     int once = 0;
  19.     String fileNameGlobal = "";
  20.     int fileSizeGlobal = 0;
  21.     boolean startThreadOnce = false;
  22.     public ServerRecieve(Socket s) throws FileNotFoundException
  23.     {
  24.         super();
  25.         this.servSocket = s;
  26.     }
  27.     public void run()
  28.     {
  29.       try
  30.       {
  31.           //ServerConnect is supposed to be started somewhere in this file
  32.           DataInputStream messagesFromServer = new DataInputStream(servSocket.getInputStream());
  33.           DataOutputStream output = new DataOutputStream(servSocket.getOutputStream());
  34.          
  35.           boolean stop = false;
  36.           while (!stop)
  37.           {
  38.              String response = messagesFromServer.readUTF();
  39.              if(response!=null && !response.isEmpty())
  40.              {
  41.                  if(response.matches(".*\\d+.*"))
  42.                  {
  43.                      System.out.println("got a number, it is: " + response);
  44.                  }
  45.                 else if(response.contains("`"))
  46.                  {
  47.                     String[] parts = response.split("`");
  48.                     String part1 = parts[0];
  49.                     //part1 contains file name....now send the file or file size
  50.  
  51.                     File file= new File(part1);
  52.                     System.out.println("File exists: " +  file.exists());
  53.                     System.out.println("File can be read: " +  file.canRead());
  54.                     System.out.println("File file.canWrite(): " +  file.canWrite());
  55.                     if ( file.exists() && file.canRead() )
  56.                     {
  57.                         // return the number of bytes in the file as a long int
  58.                         long file_size= file.length();
  59.                         System.out.println("File size is: " + file_size);
  60.                         if ( file_size > 0 )
  61.                         {
  62.                             System.out.println("432211");
  63.                             output.writeLong( file_size );
  64.                             System.out.println("File size sent.");
  65.                         }
  66.  
  67.                         else
  68.                         {
  69.                             output.writeLong( 0L );
  70.  
  71.                         }
  72.                     }
  73.                     else
  74.                     {
  75.                         output.writeLong( 0L );        
  76.  
  77.                     }
  78.                     FileInputStream file_input= new FileInputStream(file);
  79.                     System.out.println( "Transmitting file: " + part1);
  80.                     byte[] file_buffer= new byte[1500];
  81.                     int number_read;
  82.                     while( (number_read= file_input.read( file_buffer )) != -1 )
  83.                         output.write( file_buffer, 0, number_read );
  84.                     System.out.println("File " + part1 + " sent with " + number_read + " bytes read.");
  85.                    
  86.                  }
  87.                  else if(response.equals("x") || response.equals("X"))
  88.                  {
  89.                          stop = true;
  90.                          System.exit(0);
  91.                          break;
  92.                  }
  93.                  else if(!response.contains("`") && !response.equals("x") && !response.equals("X"))
  94.                  {
  95.                      System.out.println(response);
  96.                  }
  97.              }
  98.              else if(response == null)
  99.              {
  100.                  stop = true;
  101.                  System.exit(0);
  102.                  break;
  103.              }
  104.           }
  105.        }
  106.        catch (IOException e)
  107.        {
  108.            e.printStackTrace();
  109.        }
  110.       finally
  111.       {  
  112.       }
  113.     }
  114.     void OutputOptions()
  115.     {
  116.         System.out.println("Enter an option ('m', 'f', 'x'): ");
  117.         System.out.println("(M)essage (send)");
  118.         System.out.println("(F)ile (request) ");
  119.         System.out.println("e(X)it ");
  120.     }
  121.     public void sendFileSize(Socket socket4, int fileSize)
  122.     {
  123.         try
  124.         {
  125.             OutputStream os = socket4.getOutputStream();
  126.             OutputStreamWriter osw = new OutputStreamWriter(os);
  127.             BufferedWriter bw = new BufferedWriter(osw);
  128.             bw.write("File size is:" + fileSize);
  129.         }
  130.         catch (IOException e)
  131.         {
  132.             e.printStackTrace();
  133.         }
  134.         finally
  135.         {
  136.            
  137.         }
  138.    
  139.     }
  140.     public void findFile(String name,File file)
  141.     {
  142.         File[] list = file.listFiles();
  143.         if(list!=null)
  144.         for (File fil : list)
  145.         {
  146.             if (fil.isDirectory())
  147.             {
  148.                 findFile(name,fil);
  149.             }
  150.             else if (name.equalsIgnoreCase(fil.getName()))
  151.             {
  152.                 System.out.println(fil.getParentFile());
  153.             }
  154.         }
  155.     }
  156.      public int strToInt( String str )
  157.      {
  158.         int i = 0;
  159.         int num = 0;
  160.         boolean isNeg = false;
  161.         if (str.charAt(0) == '-')
  162.         {
  163.             isNeg = true;
  164.             i = 1;
  165.         }
  166.         while( i < str.length())
  167.         {
  168.             num *= 10;
  169.             num += str.charAt(i++) - '0';
  170.         }
  171.         if (isNeg)
  172.         {
  173.             num = -num;
  174.         }
  175.         return num;
  176.     }
  177. }
Add Comment
Please, Sign In to add comment