wieruowq

Server.java

Nov 1st, 2017
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. import javax.imageio.IIOException;
  5. public class Server
  6. {
  7.     private String[] serverArgs;
  8.     public Socket socket;
  9.     public Socket fileSocket;
  10.     public boolean keepRunning = true;
  11.     public int ConnectOnce = 0;
  12.     public String option = "";
  13.     public boolean isConnected = false;
  14.     public Server(String[] args) throws IOException
  15.     {
  16.         this.serverArgs = args;
  17.         if(ConnectOnce == 0)
  18.         {
  19.             int port_number1 = Integer.valueOf(serverArgs[1]);
  20.             ServerSocket serverSocket = new ServerSocket(port_number1);
  21.             socket = serverSocket.accept();
  22.             ConnectOnce = 4;
  23.             isConnected = true;
  24.         }
  25.     }
  26.     public void serverRun2(String[] args) throws IOException
  27.     {
  28.         serverArgs = args;
  29.         serverArgs = Arrays.copyOf(args, args.length);
  30.         serverSend.start();
  31.     }
  32.     Thread serverSend = new Thread()
  33.     {
  34.         public void run()
  35.         {  
  36.             OutputOptions();
  37.             while(isConnected)
  38.             {
  39.                 try
  40.                 {
  41.                    ServerRecieve serverThread = new ServerRecieve(socket);
  42.                    serverThread.start();
  43.                    DataInputStream userKeyboardInput = new DataInputStream(System.in);
  44.                    String line2 = userKeyboardInput.readLine();
  45.                    if(line2.equals("m") || line2.equals("M"))
  46.                    {
  47.                         ServerSend();
  48.                    }
  49.                    else if(line2.equals("f") || line2.equals("F"))
  50.                    {
  51.                        FileRequest();
  52.                    }
  53.                    else if(line2.equals("x") || line2.equals("X"))
  54.                    {
  55.                        isConnected = false;
  56.                        System.exit(0);
  57.                        break;
  58.                    }
  59.                    else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
  60.                            && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
  61.                    {
  62.                        System.out.println("(invalid choice)");
  63.                    }
  64.                }
  65.                catch ( Exception e )
  66.                {
  67.                    System.out.println( e.getMessage() );
  68.                }
  69.             }
  70.         }
  71.     };
  72. public void ServerSend()
  73. {
  74.         try
  75.         {
  76.             DataInputStream userKeyboardInput = new DataInputStream(System.in);
  77.             DataOutputStream output = new DataOutputStream(socket.getOutputStream());  
  78.             System.out.println("Enter your message:");
  79.             String newMessage = userKeyboardInput.readLine();
  80.             if(newMessage!=null && !newMessage.isEmpty())
  81.             {
  82.                 output.writeUTF(newMessage);
  83.             }
  84.             output.flush();
  85.             StandardInput();
  86.         }
  87.         catch (IOException e)
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.         finally
  92.         {    
  93.         }
  94. }
  95. public void FileRequest() throws FileNotFoundException
  96. {
  97.     try
  98.     {
  99.             System.out.println("Which file do you want?");
  100.          
  101.             DataInputStream userKeyboardInput = new DataInputStream(System.in);
  102.             DataOutputStream output = new DataOutputStream(socket.getOutputStream());  
  103.             String thisFileName = userKeyboardInput.readLine();
  104.             if(thisFileName!=null && !thisFileName.isEmpty())
  105.             {
  106.                 DataInputStream input = new DataInputStream(socket.getInputStream());
  107.                 output.writeUTF(thisFileName + "`" + "\n");
  108.                 long file_size= input.readLong();
  109.                 System.out.println("File size is: " + file_size);
  110.                 if ( file_size == 0 )
  111.                 {
  112.                     System.out.println("File size is 0");
  113.                 }
  114.                 FileOutputStream file_out= new FileOutputStream( thisFileName );   
  115.                 int number_read;
  116.                 byte[] buffer= new byte[1500];
  117.                 while( (number_read= input.read( buffer)) != -1 )
  118.                 {
  119.                     file_out.write( buffer, 0, number_read );
  120.                 }
  121.                 System.out.println("File " + thisFileName  + " downloaded (" + number_read + " bytes read)");
  122.                 file_out.flush();
  123.                 output.flush();
  124.                 StandardInput();
  125.             }
  126.              
  127.     }
  128.     catch (IOException e)
  129.     {
  130.         e.printStackTrace();
  131.     }
  132. }
  133. void OutputOptions()
  134. {
  135.     System.out.println("Enter an option ('m', 'f', 'x'): ");
  136.     System.out.println("(M)essage (send)");
  137.     System.out.println("(F)ile (request) ");
  138.     System.out.println("e(X)it ");
  139. }
  140. public void StandardInput()
  141. {
  142.     OutputOptions();
  143.     while(true)
  144.     {
  145.         try
  146.         {
  147.            DataInputStream userKeyboardInput = new DataInputStream(System.in);
  148.            String line2 = userKeyboardInput.readLine();
  149.            if(line2.equals("m") || line2.equals("M"))
  150.            {
  151.                 ServerSend();
  152.            }
  153.            else if(line2.equals("f") || line2.equals("F"))
  154.            {
  155.                FileRequest();
  156.            }
  157.            else if(line2.equals("x") || line2.equals("X"))
  158.            {
  159.                isConnected = false;
  160.                System.exit(0);
  161.                break;
  162.            }
  163.            else if(!line2.equals("x") && !line2.equals("X") && !line2.equals("m")
  164.                    && !line2.equals("M") && !line2.equals("f") && !line2.equals("F"))
  165.            {
  166.                System.out.println("(invalid choice)");
  167.            }
  168.        }
  169.        catch ( Exception e )
  170.        {
  171.            System.out.println( e.getMessage() );
  172.        }
  173.     }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment