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

标签 java multithreading sockets

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

请帮忙,非常感谢。

最佳答案

我在多线程客户端服务器上做了类似的尝试 file downloader应用程序。您可以在代码审查时查看它。我用过java.util.concurrent多线程位的封装类。

关于java - 并发问题 - 从 Java 多线程服务器下载的同一文件的大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595073/

相关文章:

Java定时器对象错误: Timer already cancelled

c++ - 解释双重检查锁定中的竞争条件

c - 无法理解 select() 中 write_fds 的使用 - c

java - 如何获取抛出超时异常时收到的有效负载量(Android)?

c - 文件描述符未在 exec 上关闭

java - 设置JList的最大容量?

java - 无法在 JSON 输出中获取 JPA 实体 ID

java - 为什么 CompletableFuture 在单独的流中加入/获取比使用一个流更快

python - 从线程目标访问 `self`

Promela 中的缓存模型