Java Socket File Server

Hello

I be doing this project for a few days now the only problem I have is the file that gets copy from the server to the client is 0 bytes.

This is how it works the Client enters a file name and sends it to the server than it looks for the file if it finds it sends it back to client into a folder that's hard coded.

Any help would be great.

Server Code
import java.util.*;
import java.io.*;
import java.net.*;
/**
 * The File Server
 * 
 * @author 
 * @version 2/05/2015
 */
public class Server
{
    String fileName = "";
    String path = "";
    
    /**
     * Set the file name
     * @param fileName      A file name
     */
    
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    /**
     * Get the file name
     * @return fileName
     */
    
    public String getFileName() {
        return fileName;
    }
    
    /**
     * Set the path where the files are saved to
     * 
     */
    
    public void setPath() {
        path = "ServerFile//";
    }
    
    /**
     * Get the path of the saved files
     * @return path
     */
    
    public String getPath() {
        return path;
    }
    
    /**
     * The main method to start this class
     * 
     * @param  args   Only used if we need to add variables in the cmd
     */
    
    public static void main(String[] args) {
        BufferedReader keyboard = null;
        ServerSocket serverSocket = null;
        Socket connectionSocket = null;
        File source = null;
        ObjectOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        Server newServer = new Server();
        byte[] buffer = new byte[100];
        
        try {
            serverSocket = new ServerSocket(4001);
        } catch(Exception ex) {
            System.out.println("Can't connect to port no: 5000");
        }
        
        while(true) {
            try {
                System.err.println("Part 1");
                connectionSocket = serverSocket.accept();
                System.out.println("Accept Server");
                System.out.println("Connected");
                
                System.err.println("Part 2");
                keyboard = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                System.err.println("Part 2.0");
                System.err.println("Part 2.1");
                String line = keyboard.readLine();
                System.err.println("Part 2.1.1");
                newServer.setFileName(line);
                System.err.println("Part 2.2");
                newServer.setPath();
                System.err.println("Part 2.3");
                System.err.println("Part 3");
                source = new File(newServer.getPath() + newServer.getFileName());
                System.err.println(source.toString());
                System.err.println("Part 4" + source.toString());
                outputStream = new ObjectOutputStream(connectionSocket.getOutputStream());
                outputStream.writeObject(source.getName());
                System.err.println("Part 5");
                System.err.println("Part 5.1" + source.toString());
                fileInputStream = new FileInputStream(source);
                System.err.println("Part 6");
                Integer bytesRead = 0;
                
                System.err.println("Part 7");
                
                while((bytesRead = fileInputStream.read(buffer)) > 0) {
                    System.err.println("Part 5.1");
                    outputStream.writeObject(bytesRead);
                    System.err.println("Part 5.2");
                    outputStream.writeObject(Arrays.copyOf(buffer, buffer.length));
                }
                System.err.println("Part 6");
                outputStream.close();
                System.exit(0);
            } catch(IOException ex) {
                System.out.println("IO Error"); 
            } catch(Exception ex) {
                System.err.println("Error: " + ex.getMessage());
            }
        }
    }
}



Client Code
import java.io.*;
import java.net.*;

/**
 * Teh Client class for this assignment
 * 
 * @author 
 * @version 03/05/2014
 */
public class Client
{
    String fileName = "";
    String path = "";
    
    /**
     * Set the file name for file
     * @param fileName      A string with the file name
     */
    
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    /**
     * Get the file name
     * @return fileName
     */
    
    public String getFileName() {
        return fileName;
    }
    
    /**
     * Set the path to the file
     * 
     */
    public void setPath() {
        path = "\\ClientFile\\";
    }
    
    /**
     * Get the path
     * @return path
     */
    
    public String getPath() {
        return path;
    }
    
    /**
     * main method for this class
     * @param args 
     */
    
    public static void main(String[] args) {
        Socket clientSocket = null;
        File source = null;
        BufferedReader keyboard = null;
        ObjectInputStream inputStream = null;
        ObjectOutputStream outputStream = null;
        FileOutputStream fileOutput = null;
        Object object = null;
        DataOutputStream dataOutput = null;
        final int BUFFER_SIZE = 100;
        byte[] buffer = new byte[BUFFER_SIZE];
        
        
        Client newClient = new Client();
        newClient.setPath();
        
        try {
            System.err.println("Part 1");
            clientSocket = new Socket("127.0.0.1", 4001);
        } catch(IOException ex) {
            System.err.println("Error: IO error has occuered ");
            ex.getStackTrace();
        }
        
        try {
            System.err.println("Part 2");
            keyboard = new BufferedReader(new InputStreamReader(System.in));   
        } catch(Exception ex) {
            System.err.println("Error: Unknown Error");
            ex.getStackTrace();
        }
        
        try {
            System.err.println("Part 3");
            System.out.println("Enter file to request");
            newClient.setFileName(keyboard.readLine() + "\n");
            dataOutput = new DataOutputStream(clientSocket.getOutputStream());
            dataOutput.writeBytes(newClient.getFileName());
        } catch (IOException ex) {
            System.err.println("Can't get file name");
            ex.getStackTrace();
        }
        
        try {
            System.err.println("Part 4");
            source = new File(newClient.getPath() + newClient.getFileName());
        } catch (NullPointerException ex) {
            System.err.println("Error: a file name is missing");
        } catch(Exception ex) {
            System.err.println("Error: Unknown Error");
            ex.getStackTrace();
        }
        
        try {
            System.err.println("Start of Part 5");
            inputStream = new ObjectInputStream(clientSocket.getInputStream());
            System.err.println("Part 6");
            outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
            System.err.println("Part 7");
            //1 Read file name
            object = inputStream.readObject();
            System.err.println("Part 8");
            
            System.err.println("Part 9");
            if(object instanceof String) {
                File dir = new File("ClientFile//");
                fileOutput = new FileOutputStream(object.toString());
                FileOutputStream fileOutputFile = new FileOutputStream(new File(dir, object.toString()));
                System.err.println("Part 9.1");
            } else {
                System.out.println("Something wrong");
            }
            
            // 2. Read file to the end
            Integer bytesRead = 0;
            
            System.err.println("Part 10");
            do {
                System.err.println("Part 11");
               object = inputStream.readObject();
               
               System.err.println("Part 12");
               if(!(object instanceof  Integer)) {
                   System.out.println("Something wrong");
               }
               System.err.println("Part 13");
               bytesRead = (Integer)object;
               
               System.err.println("Part 14");
               object = inputStream.readObject();
               
               System.err.println("Part 15");
               if(!(object instanceof Byte[])) {
                   System.out.println("Something wrong");
               }
               
               System.err.println("Part 16");
               buffer = (byte[])object;
               
               System.err.println("Part 17");
               // 3. write data to output file.
               outputStream.write(buffer, 0, bytesRead);
               System.err.println("Part 16");
            } while(bytesRead == BUFFER_SIZE); 
                System.out.println("Something wrong");
                inputStream.close();
                System.err.println("Part 17"); 
                fileOutput.close();
                System.err.println("Part 18");
                //outputStream.close();
                
                System.err.println("Part 19");
          
        } catch (IOException | ClassNotFoundException e) {
                System.err.println("Can't Read or Write to Dictory");
                e.getStackTrace();
        }
    }
    
}

Sign In or Register to comment.