title: Java UDP Server Programming
body: <p>I am just looking for a little guidance on a project I am working on... I have the general idea but can't seem to get code placed correctly.</p>
  
  <p>The goal is as follows:
  Write a UDP 'CompressionServer' that will take input from the user until it sees a "magic string" at which time it will create a compressed and uncompressed version of the file in the file system.</p>
  
  <p>So, I need to integrate the following "Zip" code in to my UDP server code (which already creates the uncompressed file)... That is where I am stuck at now. My first few attempts had the Zip code after I write to "fout" but that failed to create a ZIP file.</p>
  
  <p>I guess the main point here is what are the key pieces of the ZIP code that I should include and what would be the best spot to place them in my server code... Any help is appreciated!</p>
  
  <p><strong>Zip Code:</strong></p>
  
  <pre><code>import java.io.*;
  import java.util.zip.*;
  
  public class Zip {
     static final int BUFFER = 2048;
     public static void main (String argv[]) {
        try {
           String fileInput = argv[0];
           String fileOutput = argv[1];
  
           BufferedInputStream origin = null;
  
           // Create a file output stream
           FileOutputStream dest = new 
             FileOutputStream(fileOutput);
  
           ZipOutputStream out = new ZipOutputStream(new 
             BufferedOutputStream(dest));
  
           //out.setMethod(ZipOutputStream.DEFLATED);
           byte data[] = new byte[BUFFER];
  
           // get a list of files from current directory
            FileInputStream fi = new 
              FileInputStream(fileInput);
  
            origin = new 
              BufferedInputStream(fi, BUFFER);
  
            ZipEntry entry = new ZipEntry(fileInput);
            out.putNextEntry(entry);
            int count;
            while((count = origin.read(data, 0, 
              BUFFER)) != -1) {
               out.write(data, 0, count);
            }
           origin.close();
           out.close();
        } catch(Exception e) {
           e.printStackTrace();
        }
     }
  } 
  </code></pre>
  
  <p><strong>Server Code:</strong></p>
  
  <pre><code>import java.net.*;  // for DatagramSocket, DatagramPacket, and InetAddress
  import java.io.*;   // for IOException
  
  public class CompressionServer {
  
    private static final int ECHOMAX = 65535;  // Maximum size of echo datagram
    private static final int BUFFER = 2048;
  
    public static void main(String[] args) throws IOException {
  
      int servPort = Integer.parseInt(args[0]);
  
      DatagramSocket socket = new DatagramSocket(servPort);
      DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX);
  
      for (;;)/&gt;/&gt;/&gt;  {  // Run forever, receiving and echoing datagrams
            socket.receive(packet);     // Receive packet from client
        // Get data from packet
        byte[] data = packet.getData();
        // Create new string from bytes w/ offset -- contains name of file
        String fileName = new String(data, 0, packet.getLength());
        // Create file output stream w/ file name
        FileOutputStream fout = new FileOutputStream(fileName.trim());
  
        // Receive bytes from socket, write bytes to file &amp; flush
        while (true) {
          // Receive
          socket.receive(packet);
          data = packet.getData();
          String tmp = new String(data, 0, packet.getLength());
  
          // Determine if need to break from loop
          if (tmp.indexOf("--------MagicString") != -1) {
              break;
          }
  
          // Write &amp; Flush
          fout.write(data, 0, packet.getLength());
          fout.flush();
  
          }
        }
  
        // Close
        fout.close();
  
      }
      /* NOT REACHED */
    }
  }
  </code></pre>