并发问题 - 从 Java 中的多线程服务器下载的相同文件的大小不同



>我在 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();
      }
   }
}

请帮忙,提前非常感谢。

我在多线程客户端-服务器文件下载器应用程序中进行了类似的尝试。您可以在代码审查中查看它。我已经将java.util.concurrent package类用于多线程位。

最新更新