Advertisement
mrmgscott

ServerMultiClient Class

Aug 7th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. //This is each seperate connection
  2. import java.io.BufferedWriter;
  3. import java.io.EOFException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.net.Socket;
  9. import java.net.SocketException;
  10. import java.text.DateFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13.  
  14.  
  15. public class ServerMultiClient extends Thread{
  16.  
  17.     private Socket socket = null;
  18.     private ObjectOutputStream oos = null; //For SENDING data to client
  19.     ObjectInputStream ois = null; //Getting info FROM client
  20.     int serverID = -1; //ID assigned by the server Note: **If -1 is displayed in the Server Admin window, there is a problem
  21.     Object[] fromClient; //Holds data from the Client. A 2-dim array. [0] is a String tag, to identify what it is. [1] Is the data to process
  22.     private static String savedDataFileName = null; //The file name for the output file which holds the selection data from the user
  23.     private static String savedDataFileNameMouse = null; //The file name for the output file which holds the mouse data from the user
  24.     private static String ext = ".txt";
  25.  
  26.     public ServerMultiClient(Socket socket, int serverID) {
  27.         super();
  28.         this.socket = socket;
  29.         this.serverID = serverID;
  30.     }
  31.  
  32.     public void run() {
  33.         //For SENDING data to client       
  34.         try{
  35.             oos = new ObjectOutputStream(socket.getOutputStream());
  36.         }
  37.         catch(IOException e){
  38.             e.printStackTrace();
  39.         }
  40.        
  41.         //Getting info FROM client
  42.         try {
  43.             ois = new ObjectInputStream(socket.getInputStream());
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         }
  47.        
  48.  
  49.        
  50.         //A constant loop that listens for data from the client
  51.         while(true){
  52.             try{
  53.                 if((fromClient = (Object[]) ois.readObject()) != null){
  54.                     //Determine what data this is
  55.                     String tag = (String)fromClient[0]; //Getting the tag
  56.                     System.out.println("tag from client: " + tag);
  57.                     String dataStr = (String)fromClient[1];
  58.                     System.out.println("Data right from client: " + dataStr);
  59.                     if(tag.equals("ID")){
  60.                         System.out.println("Client connected");
  61.                         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
  62.                         Date date = new Date();
  63.                         savedDataFileName = "Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name
  64.                         writeSavedFile("Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date),0);
  65.                        
  66.                         //Now write the mouse coor file
  67.                         savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1]; //The default naming for the outputfile                    
  68.                         //The file will be saved with the date and time
  69.                         savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name                        
  70.                         writeSavedFile(savedDataFileNameMouse, 1);
  71.                     }
  72.                     else if(tag.equals("Selections")){
  73.                         System.out.println("Server Line: " + (String)fromClient[1]);
  74.                         writeSavedFile((String)fromClient[1], 0);
  75.                     }
  76.                     else if(tag.equals("MouseCoor")){
  77.                         System.out.println("Server Line: " + (String)fromClient[1]);
  78.                         writeSavedFile((String)fromClient[1], 1);
  79.                     }
  80.                     else{
  81.                         System.out.println("WARNING - UNKNOWN DATA RECEIVED");
  82.                     }
  83.                 }
  84.             }
  85.             catch(EOFException e){
  86.                 e.printStackTrace();
  87.             }
  88.             catch(SocketException e){
  89.                 //Server.clientDisconnected(serverID);
  90.                 break;
  91.             }
  92.             catch(Exception e){
  93.                 e.printStackTrace();
  94.             }
  95.            
  96.         }
  97.        
  98.         try{
  99.             oos.flush();
  100.             oos.close();
  101.             ois.close();
  102.             socket.close();
  103.         }
  104.         catch(Exception e){
  105.             e.printStackTrace();
  106.         }
  107.            
  108.            
  109.  
  110.            
  111.  
  112.     }
  113.    
  114.     //This method will send data to the Client
  115.     public void sendData(Object[] data){
  116.         try {
  117.             oos.writeObject(data);
  118.         } catch (IOException e) {
  119.             e.printStackTrace();
  120.         }
  121.     }
  122.    
  123.     //Write saved data: 0 = Selections  1 = Mouse Coor
  124.     public static void writeSavedFile(String line, int typeOfData){
  125.         if(typeOfData == 0){
  126.             FileWriter fstream;
  127.             try {
  128.                 System.out.println("Writing selection");
  129.                 fstream = new FileWriter(Server.getProgramPath() + savedDataFileName + ext, true); //Prepare to append (the "true") to the file
  130.                 BufferedWriter out = new BufferedWriter(fstream); //More prep
  131.                 out.write(line); //Write data
  132.                 out.newLine(); //Write a new line
  133.                 out.flush();
  134.                 out.close(); //Close the file
  135.                 fstream.close();
  136.             } catch (Exception e) {
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.         else if(typeOfData == 1){
  141.             FileWriter fstream;
  142.             try {
  143.                 System.out.println("Writing mouse");
  144.                 fstream = new FileWriter(Server.getProgramPath() + savedDataFileNameMouse + ext , true); //Prepare to append (the "true") to the file
  145.                 BufferedWriter out = new BufferedWriter(fstream); //More prep
  146.                 out.write(line); //Write data
  147.                 out.newLine(); //Write a new line
  148.                 out.close(); //Close the file
  149.                 fstream.close();
  150.             } catch (Exception e) {
  151.                 e.printStackTrace();
  152.             }
  153.         }
  154.        
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement