Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.net.URL;
- import java.net.URLDecoder;
- import javax.swing.JOptionPane;
- public class Client {
- static String id = "StackUserThanks"; //The client's ID - New one will be generated later
- private static String savedDataFileName = "testone"; //The file name for the output file which holds the selection data from the user
- private static String savedDataFileNameMouse = "testtwo"; //The file name for the output file which holds the mouse data from the user
- private static String ext = ".txt"; //The extension of the saved file
- //Server Variables//
- static Socket socket = null; //The connection to the server
- private static String defaultHost = "localhost"; //The host that will appear by default
- private static int defaultPort = 7341; //The port that will appear by default
- static ObjectInputStream ois = null; //Data being received from server
- static ObjectOutputStream oos = null; //Data being sent TO server
- Object[] fromServer; //Holds data from server. A 2-dim array. [0] a String tag, to identify what it is. [1] the data to process
- //End Server Variables
- public static void main(String[] args){
- try{
- socket = new Socket(getAddress(), getPort());
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null,
- "Could not connect to server. Call Administrator.",
- "Error",
- JOptionPane.ERROR_MESSAGE);
- e.printStackTrace();
- }
- //Data being received from Server
- try {
- ois = new ObjectInputStream(socket.getInputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- //Data being sent TO server
- try {
- oos = new ObjectOutputStream(socket.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- String[] idData = new String[2]; //Setup to send ID
- idData[0] = "ID"; //The tag for the ServerMultiClient to read
- idData[1] = id; //The information (the ID of the client)
- sendData(idData); //Send data to the server
- sendSavedData();
- }
- private static void sendSavedData(){
- try {
- System.out.println("CLIENT SEND SAVED DATA: " + getProgramPath() + savedDataFileName + ext);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- //Now send selection data to the server
- Object[] selectionData = new Object[2];
- selectionData[0] = "Selections";
- //selectionData[1] = allGraphs;
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileName + ext));
- String line = br.readLine();
- while (line != null) {
- line = br.readLine();
- selectionData[1] = line;
- System.out.println("Client Line: " + line);
- sendData(selectionData);
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //Now send selection mouse coor data to the server
- selectionData = new Object[2];
- selectionData[0] = "MouseCoor";
- selectionData[1] = "Test here 1";
- br = null;
- try {
- br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileNameMouse + ext));
- String line = br.readLine();
- while (line != null) {
- line = br.readLine();
- selectionData[1] = line;
- System.out.println("Client Line2: " + selectionData[1]);
- sendData(selectionData);
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //This method finds and returns the main program path of the main jar file.
- private static String getProgramPath() throws IOException{
- URL url = Client.class.getProtectionDomain().getCodeSource().getLocation(); //Gets the path
- String jarPath = URLDecoder.decode(url.getFile(), "UTF-8"); //Should fix it to be read correctly by the system
- String parentPath = new File(jarPath).getParentFile().getPath(); //Path of the jar
- return parentPath + File.separator;
- }
- //Gets the address from the Server text box
- private static String getAddress(){
- return defaultHost;
- }
- //Gets the port from the Port Box
- private static int getPort(){
- return defaultPort;
- }
- //Send data to the server
- private static void sendData(Object[] data){
- try {
- oos.writeObject(data);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement