7 Replies Latest reply: Sep 23, 2010 7:35 AM by 843790 RSS

    Compress data exchanged between server and client

    843790
      I have client server application, I want to compress the data exchanged between them. Not sure if I am using GZIP utilitis correctly, as there's not data exchange if I use it, the code without GZIP work correctly.
      // Server code
      
      import java.net.*;
      import java.io.*;
      import java.util.zip.*;
      
      public class SimpleServer
      {
           private Socket          socket   = null;
           private ServerSocket    server   = null;
           private DataInputStream streamIn =  null;
      
           public SimpleServer(int port)
           {
                try
                  {
                      server = new ServerSocket(port);
                      System.out.println("Server started: " + server);
                      System.out.println("Waiting for a client ...");
      
                      socket = server.accept();
                      System.out.println("Client accepted: " + socket);
      
                   streamIn = new DataInputStream(new BufferedInputStream(new GZIPInputStream(socket.getInputStream())));
                   //streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      
      
                      boolean done = false;
                      while (!done)
                      {
                          try
                          {
                               String line = streamIn.readUTF();
                                  System.out.println(line);
                                  done = line.equals("bye");
                          }
                          catch(IOException ioe)
                          {
                               done = true;
                          }
                      }
      
                   if (socket != null)    socket.close();
                   if (streamIn != null)  streamIn.close();
      
                  }
                  catch(IOException ioe)
                  {
                     System.out.println(ioe);
                  }
           }
      
      
         public static void main(String args[])
         {
              SimpleServer server = new SimpleServer(9999);
         }
      
      
      }
      
      // Client code
      import java.net.*;
      import java.io.*;
      import java.util.zip.*;
      
      public class SimpleClient
      {
           private Socket socket              = null;
           private DataInputStream  console   = null;
           private DataOutputStream streamOut = null;
      
           public SimpleClient(String serverName, int serverPort)
          {
                try
                {
                     socket = new Socket(serverName, serverPort);
                     System.out.println("Connected: " + socket);
      
                     console   = new DataInputStream(System.in);
                     streamOut = new DataOutputStream(new GZIPOutputStream(socket.getOutputStream()));
                     //streamOut = new DataOutputStream(socket.getOutputStream());
      
                     String line = "";
                     while (!line.equals("bye"))
                     {
                          try
                          {
                               line = console.readLine();
                               streamOut.writeUTF(line);
                               streamOut.flush();
                          }
                          catch(IOException ioe)
                          {
                                System.out.println("Error: " + ioe.getMessage());
                          }
                     }
      
                     if (console   != null)  console.close();
                     if (streamOut != null)  streamOut.close();
                     if (socket    != null)  socket.close();
      
                }
                catch(UnknownHostException uhe)
                {
                       System.out.println("Host unknown: " + uhe.getMessage());
                }
                catch(IOException ioe)
                {
                       System.out.println("Unexpected exception: " + ioe.getMessage());
                }
      
              }
      
      
           public static void main(String args[])
           {
              SimpleClient client = client = new SimpleClient("localhost", 9999);;
           }
      
      }
        • 1. Re: Compress data exchanged between server and client
          EJP
          You need to call GZIPOutputStream.finish() at the end of every transaction and then start using a new one, and a new GZIPInputStream at the other end. It gets a bit tricky but it can be done.

          Also put the BufferedInputStream and the GZIPInputStream the other way around, and similarly put a BufferedOutputStream underneath the GZIPOutputStream at the other end.
          • 2. Re: Compress data exchanged between server and client
            843790
            Below is the code after incorporating the suggestion, still cannot read write, but now after entering second text in client there is a exception on server - java.util.zip.ZipException: invalid block type. Please correct (if icorrectly incorporated in code) my interpretation of -> "and then start using a new one (GZIPOutputStream), and a new GZIPInputStream at the other end."
            // Server code
            import java.net.*;
            import java.io.*;
            import java.util.zip.*;
            
            public class SimpleServer
            {
                 private Socket          socket   = null;
                 private ServerSocket    server   = null;
                 //private DataInputStream streamIn =  null;
                 private DataInputStream streamIn =  null;
                 private GZIPInputStream readgzip =  null;
            
                 public SimpleServer(int port)
                 {
                      try
                        {
                            server = new ServerSocket(port);
                            System.out.println("Server started: " + server);
                            System.out.println("Waiting for a client ...");
            
                            socket = server.accept();
                            System.out.println("Client accepted: " + socket);
            
                         //streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            
                            boolean done = false;
                            while (!done)
                            {
                                readgzip = new GZIPInputStream(new BufferedInputStream(socket.getInputStream()));
                                streamIn = new DataInputStream(readgzip);
            
                                try
                                {
                                     String line = streamIn.readUTF();
                                        System.out.println(line);
                                        done = line.equals("bye");
                                }
                                catch(IOException ioe)
                                {
                                     System.out.println(ioe);
                                     done = true;
                                }
                            }
            
                         if (socket != null)    socket.close();
                         if (streamIn != null)  streamIn.close();
            
                        }
                        catch(IOException ioe)
                        {
                           System.out.println(ioe);
                        }
                 }
            
            
               public static void main(String args[])
               {
                    SimpleServer server = new SimpleServer(9999);
               }
            
            
            }
            
            
            
            // Client code 
            import java.net.*;
            import java.io.*;
            import java.util.zip.*;
            
            public class SimpleClient
            {
                 private Socket socket              = null;
                 private DataInputStream  console   = null;
                 private DataOutputStream streamOut = null;
                 private GZIPOutputStream outgzip = null;
            
                 public SimpleClient(String serverName, int serverPort)
                {
                      try
                      {
                           socket = new Socket(serverName, serverPort);
                           System.out.println("Connected: " + socket);
            
                           console     = new DataInputStream(System.in);
                           //outgzip     = new GZIPOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                           //streamOut = new DataOutputStream(outgzip);
                           //streamOut = new DataOutputStream(socket.getOutputStream());
            
                           String line = "";
                           while (!line.equals("bye"))
                           {
                                outgzip     = new GZIPOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                                streamOut = new DataOutputStream(outgzip);
                                try
                                {
                                     line = console.readLine();
                                     streamOut.writeUTF(line);
                                     streamOut.flush();
                                     outgzip.finish();
            
                                }
                                catch(IOException ioe)
                                {
                                      System.out.println("Error: " + ioe.getMessage());
                                }
                           }
            
                           if (console   != null)  console.close();
                           if (streamOut != null)  streamOut.close();
                           if (socket    != null)  socket.close();
            
                      }
                      catch(UnknownHostException uhe)
                      {
                             System.out.println("Host unknown: " + uhe.getMessage());
                      }
                      catch(IOException ioe)
                      {
                             System.out.println("Unexpected exception: " + ioe.getMessage());
                      }
            
                    }
            
            
                 public static void main(String args[])
                 {
                    SimpleClient client = client = new SimpleClient("localhost", 9999);;
                 }
            
            }
            • 3. Re: Compress data exchanged between server and client
              843790
              Got it working...
              I called finish() of GZIPOutputStream first then called flush() of DataOutputStream in my SimpleClient() and now its working.

              Thanks so much ejp !!!

              Thanks.
              • 4. Re: Compress data exchanged between server and client
                EJP
                You're creating new GZIP streams for every line. That is most unlikely to deliver any useful compression at all. If this is your actual protocol I would just forget about zipping it altogether. It's only worth it if you are shipping large transactions, tens of Kb at least.
                • 5. Re: Compress data exchanged between server and client
                  843790
                  After calling finish(), I have to use a new GZIP stream else it does not work. Hence a new GZIP stream for every line.
                  Can I use the same stream, please can you help me understand their behaviours.

                  Thanks.
                  • 6. Re: Compress data exchanged between server and client
                    EJP
                    Why? It's pointless. I've already said that. You need large amounts of data to get any advantage. You're wasting time with this.
                    • 7. Re: Compress data exchanged between server and client
                      843790
                      This code is a demo code which helped me understand how GZIP is used and its functioning, so that it can be applied on large data transaction efficiently.
                      Hence, I wanted to understand it thoroughly.

                      Thanks.