views:

18

answers:

0

here is the code on unix box(client):

        else if (strIn.startsWith("GET")) {
            PrintLog("GET command received...");                

            //COMPRESSION
            String filename = parameter;
            File getfile = new File(filename);
            if (getfile.exists())
            {
            PrintLog("Compressing..."); 
            Calendar calendar = Calendar.getInstance();
            tempCFile = "temp"+ calendar.get(Calendar.HOUR)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND)+calendar.get(Calendar.MILLISECOND);

            // Create a buffer for reading the files
            byte[] buf = new byte[1024];

            try {
                // Create the ZIP file
                String outFilename = tempCFile;
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

                // Compress the files
                FileInputStream in = new FileInputStream(filename);

                // Add ZIP entry to output stream.
                filename = new File(filename).getName();
                out.putNextEntry(new ZipEntry(filename));

                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();

                // Complete the ZIP file
                out.close();
            } catch (IOException e) { e.printStackTrace();  }

            //END COMPRESSION



            //File file = new File(parameter); //OLD WAY
            File file = new File(tempCFile);
            PrintLog("Sending: " + file.length() + " bytes");                                                                       
            //send FILE SIZE
            dos.writeBytes(new String("" + file.length()));
            //dos.writeByte(-1);
            dos.flush();

            //send FILE

            if (file.length() > 0) {
                FileInputStream fstream = new FileInputStream(file);                                                
                final int TxBuffer = 65536;
                long fileLength = file.length();
                int[] fileBytes = new int[TxBuffer];
                try {   
                    for (long i = fileLength; i > 0;) {                                 
                        if ( i > TxBuffer) {
                            for( int b = 0; b < TxBuffer; b++) {
                                fileBytes[b] = fstream.read();
                            }
                            for( int b = 0; b < TxBuffer; b++) {
                                dos.writeByte(fileBytes[b]);
                            }
                        }
                        else {
                            fileBytes = new int[(int)i];
                            for ( int b = 0; b < i; b++) {
                                fileBytes[b] = fstream.read();
                            }
                            for ( int b = 0; b < i; b++) {
                                dos.writeByte(fileBytes[b]);
                            }
                        }
                        i = i - TxBuffer;
                    }                       

                    //dos.writeByte(-1);
                    dos.flush();            
                    fstream.close();

                //strOut = new String("DONE! GET command completed...");
                } catch (Exception e) { 
                    PrintLog("Get Error sending file: " + e.toString());
                    dos.close();
                    fstream.close();
                    socket.close(); 
                }
              }
            }
            else 
                PrintLog("Get: file not found !"); 
            dos.writeBytes(strOut);
            //dos.writeByte(-1);
            dos.flush();
            dos.close();                
            socket.close();         
            PrintLog("GET done Connection closed...");              
        }

CODE on WINDOWS SERVER 2003:

                if (client.Connected)
                    stream.Flush();
                Console.WriteLine("NetGrabber GET command received " + command.ToString());
                byte[] buffer = new byte[client.ReceiveBufferSize];
                int count = 0;
                if (client.Connected)
                    count = stream.Read(buffer, 0, buffer.Length);
                Console.WriteLine("NetGrabber GET Read count = " + count); //output the file size


                if (buffer != null)
                {
                    string length = Encoding.UTF8.GetString(buffer, 0, count).ToString();
                    Console.WriteLine("NetGrabber GET fileLength = [" + length + "]"); //output the file size
                    fileLength = Int32.Parse(length);
                }
            } catch (Exception e) {
                throw new Exception("NetGrabber Error in GET: Exception (" + hostname + ") Error: " + e.ToString());
            }
            //stream.Flush();

P.S. I asked the same question before, without the code. So it was closed. So, i've opened a fresh question.