title: Java TCP file transfer application
body: <p>Hello and thanks in advance for taking the time to read. I'm trying to develop a simple Java file transfer application using TCP. So far using my client and server classes I've managed to add a working upload method to it (upload a file to the computer where the server's running).</p>
  
  <p>I'm quite inexperienced with this yet so I'm going rather slowly. I also need to add a download option (get a file from the server), list all the files from the server, and also an option that allows me to delete any file from the server.</p>
  
  <p>For both the server and the client I'm using a specific default folder (C:/Sharedfolder in this case). Currently my questions are:</p>
  
  <ol>
  <li>In my current server code, files that are uploaded to it are renamed to "newfile". How can I make it change the filename to something like "oldfile_new" (being oldfile the actual original file's name)?</li>
  <li>Using my current server code below, can I make a client class for downloading a file from the server that will work with it? And how do I do that? The uploading part made sense to me, but I'm still trying to figure out the downloading/listing files from the server.</li>
  <li>I'm currently using a predefined host IP, but I'll eventually need to modify the application so that the client sends out a UDP broadcast to find the server on a private network. Can anyone point me on any direction on how to do this?</li>
  </ol>
  
  <p>My current server code is as follows:</p>
  
  <pre><code>public class Server {
  
  public static void main(String[] args) throws IOException {
  
      ServerSocket serverSocket = null;
  
      try {
          serverSocket = new ServerSocket(4555);
      } catch (IOException ex) {
          System.out.println("Can't setup server on this port number.\n");
      }
  
      Socket socket = null;
      InputStream is = null;
      OutputStream fos = null;
      BufferedOutputStream bos = null;
      int bufferSize = 0;
      System.out.println("Ready...");
  
      try {
          socket = serverSocket.accept();
      } catch (IOException ex) {
          System.out.println("Can't accept client connection.\n");
      }
  
      try {
          is = socket.getInputStream();
  
          bufferSize = socket.getReceiveBufferSize();
          System.out.println("Buffer size: " + bufferSize);
      } catch (IOException ex) {
          System.out.println("Can't get socket input stream.\n");
      }
  
      try {
          fos = new FileOutputStream("C:\\Sharedfolder\\newfile");
          bos = new BufferedOutputStream(fos);
  
      } catch (FileNotFoundException ex) {
          System.out.println("File not found.\n");
      }
  
      byte[] bytes = new byte[bufferSize];
  
      int count;
  
      while ((count = is.read(bytes)) &gt; 0) {
          bos.write(bytes, 0, count);
      }
  
      bos.flush();
      bos.close();
      is.close();
      socket.close();
      serverSocket.close();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any help would be much appreciated. Thanks :)</p>