import javax.swing.*;
import java.net.*;
import java.io.*;
/**
* Start of the Connect class
* @author Andrew Gisler, Matthew Clark, Matthew George
*/
public class Connect {
Socket socket; //create a socket variable
BufferedReader in; //create a BufferedReader variable
PrintWriter out; //create a PrintWriter variable
/**
* constructor of Connect
*/
private boolean status;
public Connect() {
}
/**
* This method starst the connection to the server.
* It also displays errors if connection was unsuccessful or if an invalid host
* name was entered
* @param address this is the server address the user entered in
* @param port this is the port number the user entered in
* @return if the connection was successful or failed
* @throws IOException
*/
public boolean startConnection(String address, String port) throws IOException {
Client cl = new Client(); //create new instance of the Client class
boolean status = false; //set the status of the connection to false
try {
InetAddress inet = InetAddress.getByName(address); //check if the address is valid
socket = new Socket(inet, Integer.parseInt(port)); //create sockeet
status = true; //set status to true
} catch (UnknownHostException ex) { //catch if host name is invalid
JOptionPane.showMessageDialog(cl, "Invalid host: Please check host name"); //displays error for invalid host
} catch (ConnectException ex) { //catch if connection was unsuccessful
JOptionPane.showMessageDialog(cl, "Can't Connect - try again"); //displays error for connection failure
} catch (IOException ex) {
}
return status; //return status of connection to client class
}
/**
* This method gets the file from the server.
* @param fileName is the selected filename the user wants from the server
* @param path is the location of where the file should be created
* @return the statistics of the tranfer to the client class
* @throws IOException
*/
public String getFile(String fileName, String path) throws IOException {
String newFileName = fileExists(path,fileName);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //create a new instance of PrintWriter based on the socket connection
out.println("open");
out.flush();
out.println(fileName); //send out the filname across the socket to the server
out.flush();
String filesize = in.readLine(); //get the size of the file from the server
long fileSize = Long.parseLong(filesize.trim()); //convert the filesize from a string to a long datatype
DataInputStream dataInput = new DataInputStream(socket.getInputStream()); //create a new instance of a DataInputStream in order to get data from server
FileOutputStream fileOutput = new FileOutputStream(path.trim() + "\\" + newFileName.trim()); //create new instance of FileOutputStream to write the file to the clients computer
byte[] bb = new byte[10000]; //create a new instance of a byte
while (true) {
int result = dataInput.read(bb); //read 10000 bytes and place bytes in variable called result
fileOutput.write(bb, 0, result); //write a file on input
if (fileSize <= result) { //check if result is equal to the filesize
break; //break while loop
}
fileSize -= result; //subtract result from fileSize
}
int result2 =dataInput.read(bb);
String info = in.readLine(); //read in the statistics for the transfer fromt the server
return info; //return the statistics to the client
}
/**
* This method is used to check if the destination of images already contains
* the file names. If the file already exists then it adds a number to the name
* to make the file transfer unique
* @param path is the selected path from the user
* @param fileName is the filename the user would like
* @return the new filename
*/
public String fileExists(String path, String fileName) {
File fileCheck = new File(path.trim() + "\\" + fileName.trim()); //make a new of the file
int count = 0; //create count and set to 0
String newFileName = ""; //set newFilename to ""
String[] nameSplit =fileName.split("\\."); // split the fileName and the extention of the name into a String array
File folder = new File(path); //get directory of files set by the user
File[] listOfFiles = folder.listFiles(); //get list of files in the directory
if (fileCheck.exists()) { //check if origanl file exists
} else {
newFileName = fileName; //copy name of origanl file to the newFilename string
}
for (int i = 0; i < listOfFiles.length; i++) { //create loop to check files validate
String listName = listOfFiles[i].getName(); //get the name of the file in the directory
String[] fileSplit = listName.split("\\."); //split the name from the extension into an array
boolean containsNumber = fileSplit[0].contains(Integer.toString(i)); //check if the name contains an integer
boolean containsName = fileSplit[0].contains(fileName); //check if the name contains the name
if (containsName==true) //check if the name is true
if (containsNumber==true) { //check if there is a number
count++; //increment the count
}
}
if (newFileName.equals(fileName)){ //check if fileName equals newFileName
}else
newFileName = nameSplit[0] + "(" + Integer.toString(count) + ")" + "." + nameSplit[1]; //add the count to the filename
return newFileName; //return the newFileName
}
/**
* This method gets list of files fromt the server
* @return the list of files to the client class
* @throws IOException
*/
public String getList() throws IOException {
String arrayFiles = null; //create new array and set to null
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //create new buffered reader
try {
arrayFiles = in.readLine(); //get list of files
} catch (NumberFormatException nFE) {
}
return arrayFiles; //return list of files to client class
}
/**
* close the connection when the clients disconnect button is pressed or
* window is closed
* @throws IOException
*/
public void closeConnection() throws IOException {
try {
if (socket.isConnected() == true) { //check if the socket is connected
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println("close"); //send message to server to close socket
socket.close(); //close socket
}
} catch (RuntimeException e) {
}
}
}