Advertisement
mrmgscott

Client Class

Aug 7th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.net.Socket;
  8. import java.net.URL;
  9. import java.net.URLDecoder;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13.  
  14. public class Client {
  15.    
  16.     static String id = "StackUserThanks"; //The client's ID - New one will be generated later
  17.     private static String savedDataFileName = "testone"; //The file name for the output file which holds the selection data from the user
  18.     private static String savedDataFileNameMouse = "testtwo"; //The file name for the output file which holds the mouse data from the user
  19.     private static String ext = ".txt"; //The extension of the saved file
  20.    
  21.     //Server Variables//
  22.     static Socket socket = null; //The connection to the server
  23.     private static String defaultHost = "localhost"; //The host that will appear by default
  24.     private static int defaultPort = 7341; //The port that will appear by default
  25.     static ObjectInputStream ois = null; //Data being received from server
  26.     static ObjectOutputStream oos = null; //Data being sent TO server
  27.     Object[] fromServer; //Holds data from server. A 2-dim array. [0] a String tag, to identify what it is. [1] the data to process
  28.     //End Server Variables
  29.    
  30.     public static void main(String[] args){
  31.         try{
  32.             socket = new Socket(getAddress(), getPort());
  33.         }
  34.         catch(Exception e){
  35.             JOptionPane.showMessageDialog(null,
  36.                     "Could not connect to server. Call Administrator.",
  37.                     "Error",
  38.                     JOptionPane.ERROR_MESSAGE);
  39.             e.printStackTrace();
  40.         }
  41.        
  42.         //Data being received from Server
  43.         try {
  44.             ois = new ObjectInputStream(socket.getInputStream());
  45.         } catch (IOException e) {
  46.             e.printStackTrace();
  47.         }
  48.        
  49.         //Data being sent TO server
  50.         try {
  51.             oos = new ObjectOutputStream(socket.getOutputStream());
  52.         } catch (IOException e) {
  53.             e.printStackTrace();
  54.         }
  55.        
  56.         String[] idData = new String[2]; //Setup to send ID
  57.         idData[0] = "ID"; //The tag for the ServerMultiClient to read
  58.         idData[1] = id; //The information (the ID of the client)
  59.         sendData(idData); //Send data to the server
  60.        
  61.         sendSavedData();
  62.     }
  63.    
  64.     private static void sendSavedData(){
  65.         try {
  66.             System.out.println("CLIENT SEND SAVED DATA: " + getProgramPath() + savedDataFileName + ext);
  67.         } catch (IOException e1) {
  68.             e1.printStackTrace();
  69.         }
  70.         //Now send selection data to the server
  71.         Object[] selectionData = new Object[2];
  72.         selectionData[0] = "Selections";
  73.         //selectionData[1] = allGraphs;
  74.         BufferedReader br = null;
  75.         try {
  76.             br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileName + ext));
  77.             String line = br.readLine();
  78.             while (line != null) {
  79.                 line = br.readLine();
  80.                 selectionData[1] = line;
  81.                 System.out.println("Client Line: " + line);
  82.                 sendData(selectionData);
  83.                
  84.             }
  85.         }
  86.         catch(Exception e){
  87.             e.printStackTrace();
  88.         }
  89.         finally {
  90.             try {
  91.                 br.close();
  92.             } catch (IOException e) {
  93.                 e.printStackTrace();
  94.             }
  95.         }
  96.        
  97.        
  98.         //Now send selection mouse coor data to the server
  99.         selectionData = new Object[2];
  100.         selectionData[0] = "MouseCoor";
  101.         selectionData[1] = "Test here 1";
  102.         br = null;
  103.         try {
  104.             br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileNameMouse + ext));
  105.             String line = br.readLine();
  106.             while (line != null) {
  107.                 line = br.readLine();
  108.                 selectionData[1] = line;
  109.                 System.out.println("Client Line2: " + selectionData[1]);
  110.                 sendData(selectionData);
  111.  
  112.             }
  113.         }
  114.         catch(Exception e){
  115.             e.printStackTrace();
  116.         }
  117.         finally {
  118.             try {
  119.                 br.close();
  120.             } catch (IOException e) {
  121.                 e.printStackTrace();
  122.             }
  123.         }
  124.     }
  125.    
  126.     //This method finds and returns the main program path of the main jar file.
  127.     private static String getProgramPath() throws IOException{
  128.         URL url = Client.class.getProtectionDomain().getCodeSource().getLocation(); //Gets the path
  129.         String jarPath = URLDecoder.decode(url.getFile(), "UTF-8"); //Should fix it to be read correctly by the system
  130.         String parentPath = new File(jarPath).getParentFile().getPath(); //Path of the jar
  131.         return parentPath + File.separator;
  132.     }
  133.    
  134.     //Gets the address from the Server text box
  135.     private static String getAddress(){
  136.         return defaultHost;
  137.     }
  138.    
  139.     //Gets the port from the Port Box
  140.     private static int getPort(){
  141.         return defaultPort;
  142.     }
  143.    
  144.     //Send data to the server
  145.     private static void sendData(Object[] data){
  146.         try {
  147.             oos.writeObject(data);
  148.         } catch (IOException e) {
  149.             e.printStackTrace();
  150.         }
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement