0

我已经用 Java 实现了一个用于下载文件的多线程客户端/服务器程序,其中服务器可以通过利用线程同时将文件提供给许多客户端。

当我使用单个客户端测试服务器时,它工作正常,但是当我使用 shell 脚本对十个或更多客户端进行测试时,下载的文件都有不同的大小,这与服务器端文件的实际大小不同。

谁能解释为什么会这样?

服务器代码:

public class FileSend implements Runnable {
   Socket sock;
   String pathname;

   FileSend(Socket s, String filename) {
      sock = s;
      pathname = System.getenv("HOME") + "/" + Main.spath + "/" + filename;
   }

   void send(String pathname) throws IOException, NoSuchAlgorithmException,
         NoSuchPaddingException, ParseException {

      try {
         byte[] buf = new byte[1024];
         OutputStream os = sock.getOutputStream();
         //PrintWriter writer = new PrintWriter(os);
         BufferedOutputStream out = new BufferedOutputStream(os, 1024);
         int i = 0;
         File fp = new File(pathname);
         RandomAccessFile ra = new RandomAccessFile(fp, "r");
         long bytecount = 1024;
         ////////////////

         while ((i = ra.read(buf, 0, 1024)) != -1) {

            bytecount += 1024;
            out.write(buf, 0, i);
            out.flush();
         }
         sock.shutdownOutput();
         out.close();
         ra.close();
         sock.close();
      } catch (IOException ex) {

      }
   }

   public void run() {
      try {
         try {
            try {
               try {
                  send(this.pathname);
               } catch (ParseException ex) {
                  Logger.getLogger(FileSend.class.getName()).log(Level.SEVERE,
                        null, ex);
               }
            } catch (NoSuchPaddingException ex) {
               Logger.getLogger(FileSend.class.getName()).log(Level.SEVERE,
                     null, ex);
            }
         } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(FileSend.class.getName()).log(Level.SEVERE, null,
                  ex);
         }
      } catch (IOException ex) {
         Logger.getLogger(FileSend.class.getName()).log(Level.SEVERE, null, ex);
      }
   }
}

客户代码:

public class FileRecieve implements Runnable {
   Socket sock;
   String path;
   Date T;

   //private Date d2;
   FileRecieve(Socket s, String fname, Date d1) {
      sock = s;
      path = Main.Dpath + "/" + fname;
      T = d1;
   }

   public void run() {
      try {
         byte[] b = new byte[1024];
         int len = 0;
         long bytcount = 1024;

         File fp = new File(path);
         // RandomAccessFile ra = new RandomAccessFile(fp,"r");

         RandomAccessFile ra = new RandomAccessFile(fp, "rw");
         ra.seek(0);
         InputStream is = sock.getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         while ((len = is.read(b, 0, 1024)) != -1) {
            bytcount = bytcount + 1024;

            //decrypt
            ra.write(b, 0, len);
         }

         is.close();
         ra.close();
         sock.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

请帮助,非常感谢提前。

4

1 回答 1

1

I have made a similar attempt at a Mutithreaded Client-Server file downloader app. You can check it out at code review. I have used the java.util.concurrent package classes for the multithreading bit.

于 2012-11-28T15:38:44.730 回答