Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is each seperate connection
- import java.io.BufferedWriter;
- import java.io.EOFException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.net.SocketException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class ServerMultiClient extends Thread{
- private Socket socket = null;
- private ObjectOutputStream oos = null; //For SENDING data to client
- ObjectInputStream ois = null; //Getting info FROM client
- int serverID = -1; //ID assigned by the server Note: **If -1 is displayed in the Server Admin window, there is a problem
- 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
- private static String savedDataFileName = null; //The file name for the output file which holds the selection data from the user
- private static String savedDataFileNameMouse = null; //The file name for the output file which holds the mouse data from the user
- private static String ext = ".txt";
- public ServerMultiClient(Socket socket, int serverID) {
- super();
- this.socket = socket;
- this.serverID = serverID;
- }
- public void run() {
- //For SENDING data to client
- try{
- oos = new ObjectOutputStream(socket.getOutputStream());
- }
- catch(IOException e){
- e.printStackTrace();
- }
- //Getting info FROM client
- try {
- ois = new ObjectInputStream(socket.getInputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- //A constant loop that listens for data from the client
- while(true){
- try{
- if((fromClient = (Object[]) ois.readObject()) != null){
- //Determine what data this is
- String tag = (String)fromClient[0]; //Getting the tag
- System.out.println("tag from client: " + tag);
- String dataStr = (String)fromClient[1];
- System.out.println("Data right from client: " + dataStr);
- if(tag.equals("ID")){
- System.out.println("Client connected");
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
- Date date = new Date();
- savedDataFileName = "Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name
- writeSavedFile("Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date),0);
- //Now write the mouse coor file
- savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1]; //The default naming for the outputfile
- //The file will be saved with the date and time
- savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name
- writeSavedFile(savedDataFileNameMouse, 1);
- }
- else if(tag.equals("Selections")){
- System.out.println("Server Line: " + (String)fromClient[1]);
- writeSavedFile((String)fromClient[1], 0);
- }
- else if(tag.equals("MouseCoor")){
- System.out.println("Server Line: " + (String)fromClient[1]);
- writeSavedFile((String)fromClient[1], 1);
- }
- else{
- System.out.println("WARNING - UNKNOWN DATA RECEIVED");
- }
- }
- }
- catch(EOFException e){
- e.printStackTrace();
- }
- catch(SocketException e){
- //Server.clientDisconnected(serverID);
- break;
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- try{
- oos.flush();
- oos.close();
- ois.close();
- socket.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- //This method will send data to the Client
- public void sendData(Object[] data){
- try {
- oos.writeObject(data);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //Write saved data: 0 = Selections 1 = Mouse Coor
- public static void writeSavedFile(String line, int typeOfData){
- if(typeOfData == 0){
- FileWriter fstream;
- try {
- System.out.println("Writing selection");
- fstream = new FileWriter(Server.getProgramPath() + savedDataFileName + ext, true); //Prepare to append (the "true") to the file
- BufferedWriter out = new BufferedWriter(fstream); //More prep
- out.write(line); //Write data
- out.newLine(); //Write a new line
- out.flush();
- out.close(); //Close the file
- fstream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- else if(typeOfData == 1){
- FileWriter fstream;
- try {
- System.out.println("Writing mouse");
- fstream = new FileWriter(Server.getProgramPath() + savedDataFileNameMouse + ext , true); //Prepare to append (the "true") to the file
- BufferedWriter out = new BufferedWriter(fstream); //More prep
- out.write(line); //Write data
- out.newLine(); //Write a new line
- out.close(); //Close the file
- fstream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement