Advertisement
Guest User

ServerSource

a guest
Mar 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1.  
  2. package clientserver;
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.*;
  6. import java.util.concurrent.ExecutorService;
  7.  
  8. public class Server implements Runnable {
  9.    
  10.     String productFile="Record.txt";
  11.     Socket socketThreaded;
  12.  
  13.  
  14.     // Instantiate ServerSocket for connection to server.
  15.     public ServerSocket startSocket(){
  16.        
  17.         try{
  18.         ServerSocket ssocket = new ServerSocket(9001);
  19.         System.out.println(ssocket);
  20.         return ssocket;
  21.                
  22.         }catch(Exception e){
  23.             System.out.println("Exception: " + e);
  24.             return null;
  25.         }
  26.     }
  27.  
  28.     // Method to connect socket to client. Takes server socket as an argument.
  29.     public Socket socketConnect(ServerSocket server){
  30.         try{
  31.             System.out.println("Waiting for connection from client....");
  32.             Socket s = server.accept();
  33.             System.out.println(s);
  34.             return s;
  35.            
  36.            
  37.         }catch(Exception e){
  38.             System.out.println("Exception: " + e);
  39.             return null;
  40.         }
  41.        
  42.     }
  43.  
  44.     // Reader class to interpret and return lines from the record, productFile
  45.     synchronized public String reader(int recordLine) throws FileNotFoundException {
  46.         Scanner reader = new Scanner(new File(productFile));
  47.         int index=0;
  48.        
  49.         while(reader.hasNext()){
  50.             System.out.println(recordLine);
  51.             String record = reader.nextLine();
  52.             if (index==recordLine){
  53.                 return record;}
  54.             index++;
  55.         }
  56.        
  57.         return "EOF";
  58.     }
  59.  
  60.     // Method to return the amount of records in productFile
  61.     synchronized public int recordCount()throws FileNotFoundException{
  62.         Scanner scan = new Scanner(new File(productFile));
  63.         int index=0;
  64.         while(scan.hasNext()){
  65.             scan.nextLine();
  66.             index++;
  67.         }
  68.         return index;
  69.     }
  70.  
  71.     // Method to update a specific row in the record, productFile
  72.     synchronized public void updateRow(int row,String data){
  73.         try{
  74.            
  75.         Scanner scan = new Scanner(new File(productFile));
  76.         FileWriter writer = new FileWriter(productFile, false);
  77.         List<String> allRows = new ArrayList<String>();
  78.        
  79.         int index = 0;
  80.         while(scan.hasNext()){
  81.             String rec = scan.nextLine();
  82.             allRows.add(rec);
  83.             System.out.println("List Index: " + allRows.get(index));
  84.         }
  85.         scan.close();
  86.         allRows.remove(row);
  87.         allRows.add(row,data);
  88.        
  89.         for(String rowx : allRows){
  90.             writer.write(rowx + "\r\n");
  91.             System.out.println(rowx);
  92.         }
  93.         writer.close();
  94.        
  95.         }catch(Exception e){
  96.             System.out.println("Update Exception! " + e);
  97.             e.printStackTrace();
  98.  
  99.         }
  100.     }
  101.  
  102.     // Method to listen for commands from established client socket 's'. Waits for commands from client and returns based on conditional.
  103.     synchronized public void listen(Socket s){
  104.        
  105.         try{
  106.         ObjectInputStream input = new ObjectInputStream(s.getInputStream());
  107.         ObjectOutputStream output = new ObjectOutputStream(s.getOutputStream());
  108.        
  109.         System.out.println("Welcome Check");
  110.         output.writeObject("Welcome, we are now connected.");
  111.         String command="";
  112.         int recordLine=0;
  113.        
  114.         while(command.equals("exit")==false){
  115.            
  116.             System.out.println("Now listening for commands...");
  117.             command = (String)input.readObject();
  118.             System.out.println("Received command: " + command);
  119.            
  120.            
  121.             if(command.equals("Retrieve")){
  122.                 System.out.println("Retrieved Record!");
  123.                 String record = reader(recordLine);
  124.                 System.out.println("Current row's value is: " + reader(recordLine));
  125.                 output.writeObject(record);
  126.                 output.flush();}
  127.             else if(command.equals("Next")){
  128.                 recordLine++;
  129.                 System.out.println("Retrieved Record!");
  130.                 String record = reader(recordLine);
  131.                 if(record.equals("EOF")){
  132.                     recordLine=0;
  133.                     record = reader(recordLine);}
  134.                 System.out.println("Current row's value is: " + reader(recordLine));
  135.                 output.writeObject(record);
  136.                 output.flush();}
  137.             else if(command.equals("Previous")){
  138.                 recordLine--;
  139.                 System.out.println("Retrieved Record!");
  140.                 String record = reader(recordLine);
  141.                 if(recordLine < 0){
  142.                     recordLine = recordCount() - 1;
  143.                     record = reader(recordLine);}
  144.                 System.out.println("Current row's value is: " + reader(recordLine));
  145.                 output.writeObject(record);
  146.                 output.flush();}
  147.             /*
  148.             else if(command.equals("Update")){
  149.                 System.out.println(recordLine);
  150.                 String newField = (String)input.readObject();
  151.                 updateRow(recordLine,newField);
  152.             }*/
  153.         }
  154.        
  155.        
  156.        
  157.         }catch(Exception e){
  158.             System.out.println("Exception: " + e);
  159.         }
  160.     }
  161.    
  162.    
  163.     @Override
  164.     public void run(){
  165.         listen(socketThreaded);
  166.     }
  167.    
  168.     public static void main(String [] args){
  169.        
  170.         //Variable declaration for runtime.
  171.         Server server = new Server();
  172.         ServerSocket ss = (server.startSocket());
  173.        
  174.        
  175.         //Utilization of methods for connecting server socket to client socket, and acception messages & commands.
  176.         while(true){
  177.            
  178.         Socket s = server.socketConnect(ss);
  179.        
  180.         Server serverThreaded = new Server();
  181.         serverThreaded.socketThreaded = s;
  182.        
  183.         System.out.println("This Socket Is: " + serverThreaded.socketThreaded);
  184.         ExecutorService serv = java.util.concurrent.Executors.newCachedThreadPool();
  185.         serv.execute(serverThreaded);
  186.        
  187.        
  188.        
  189.         }
  190.        
  191.     }
  192.    
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement