How to transfer directories with their files over a socket connection using Java?
<p>I need to know how to transfer directories and with their files over a socket connection.  The way I have it set up right now, it recursively goes through all of the folders and sends them to the server, where it then saves them.  It makes them all into one big mess, and I want them all to be in the correct folders.  Here is what I have:</p>

<p>Server:</p>

<pre><code>package com.charsmud.gmail.server;

import java.net.*;     
import java.io.*;     

public class Server {      

    public static void main (String[] args )
    {     
      backup();
    }
        public static void backup()
        {
            try
            {
                int bytesRead;  

                ServerSocket serverSocket  = new ServerSocket(69);

                while(true) {  
                    Socket clientSocket = null;  
                    clientSocket = serverSocket.accept();  

                    InputStream in = clientSocket.getInputStream();  

                    DataInputStream clientData = new DataInputStream(in);   

                    String fileName = clientData.readUTF(); 

                    File f = new File("C:/Users/Will/Desktop/FTP/" + fileName);

                    OutputStream output = new FileOutputStream(f);     
                    long size = clientData.readLong();     
                    byte[] buffer = new byte[1024];     
                    while (size &gt; 0 &amp;&amp; (bytesRead = clientData.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1)     
                    {     
                        System.out.println(clientData.read());
                        output.write(buffer, 0, bytesRead);     
                        size -= bytesRead;     
                    }  

                    // Closing the FileOutputStream handle  
                    output.close();  
                }  
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }

    }  
}
</code></pre>

<p>Client:</p>

<pre><code>package com.charsmud.gmail.client;

import java.net.*;  
import java.io.*;  

public class Client {  

    public static void main(String[] args)
    {  
        ClientGUI gui = new ClientGUI();
        gui.gui();
    }  
    public static void fileTransfer(File[] files) throws IOException
    {
        System.out.println(1);

        for(File file : files)
        {
            if(file.isDirectory())
            {

                System.out.println("Directory: " + file.getName());
                fileTransfer(file.listFiles());
            }
            else
            {
                System.out.println(file.getPath());
                System.out.println(file.getAbsolutePath());
                System.out.println(file.getName());
                sendFiles(file.getPath());
            }
        }
    }
    public static void sendFiles(String backup)
    {
        try
        {
            File f = new File(backup);
            Socket sock = new Socket("localhost", 69);  
            System.out.println(2);
            //Send file  
            byte[] mybytearray = new byte[(int) f.length()];  
            FileInputStream fis = new FileInputStream(f);  
            BufferedInputStream bis = new BufferedInputStream(fis);  
            //bis.read(mybytearray, 0, mybytearray.length);  
            System.out.println(3);
            DataInputStream dis = new DataInputStream(bis);     
            dis.readFully(mybytearray, 0, mybytearray.length);  

            OutputStream os = sock.getOutputStream();  

            //Sending file name and file size to the server  
            DataOutputStream dos = new DataOutputStream(os);     
            dos.writeUTF(f.getName());     
            dos.writeLong(mybytearray.length);     
            dos.write(mybytearray, 0, mybytearray.length);     
            dos.flush();  

            //Sending file data to the server  
            //os.write(mybytearray, 0, mybytearray.length);  
            //os.flush();  

            //Closing socket  
            sock.close();  
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
}  
</code></pre>

<p>GUI:</p>

<pre><code>package com.charsmud.gmail.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ClientGUI implements ActionListener{

    JFrame frame;
    JPanel panel;
    JLabel backupLabel;
    JTextField backupField;
    JButton transfer;

    public void gui()
    {
         frame = new JFrame("Charsmud Wireless Backup");
         panel = new JPanel();

         backupLabel = new JLabel("Backup File");

         backupField = new JTextField(34);

         backupField.setText("C:/");
         backupField.setEditable(true);

         transfer = new JButton("Backup and Transfer");

         transfer.addActionListener(this);

         panel.add(backupLabel);
         panel.add(backupField);
         panel.add(transfer);

         frame.setVisible(true);
         frame.setSize(500, 500);

         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

         frame.add(panel);
    }

    public void actionPerformed(ActionEvent evt)
    {
        Client c = new Client();
        try 
        {
            File[] f = new File(backupField.getText()).listFiles();
            c.fileTransfer(f);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>Any help?</p>
