Java - 下载

时间:2015-06-10 04:55:21

标签: java stream

我的程序只能进行1次交易 下载或上传 但不能 下载然后上传

服务器:

    ServerSocket serverSocket = new ServerSocket(4444);
    Socket socket = serverSocket.accept();

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream("E:\\23c-dl.mkv"));


    int count;
    byte[] bytes = new byte[8192];

    while ((count = is.read(bytes)) > 0) {
        bos.write(bytes, 0, count);
        System.out.println("-->" + count);
    }

    System.out.println("DONE DOWNLOADING");


    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23s.mkv")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count2;
    byte[] bytes2 = new byte[8192];

    while ((count2 = fileInput.read(bytes2)) > 0) {
        out.write(bytes2, 0, count2);
        System.out.println("--> " + count2);
    }

    /* CLOSE */
    socket.close();
    serverSocket.close();

客户

    Socket socket = new Socket("127.0.0.1", 4444);

    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23c.mkv")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count;        
    byte[] bytes = new byte[8192];

    while ((count = fileInput.read(bytes)) > 0) {
        out.write(bytes, 0, count);
        System.out.println("-->" + count);
    }
    System.out.println("DONE UPLOADING");

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\23s-dl.mkv"));

    int count2; 
    byte[] bytes2 = new byte[8192];

    while ((count2 = is.read(bytes2)) > 0) {
        bos.write(bytes2, 0, count2);
        System.out.println("-->" + count2);
    }

    /* CLOSE */
    socket.close();

如果我注释掉服务器的获取和客户端的发送:程序有效。服务器可以下载文件。

如果我注释掉服务器的发送和客户端的获取:程序有效。 客户可以下载该文件。

不评论任何内容:只有服务器可以下载该文件。 有什么问题?

1 个答案:

答案 0 :(得分:1)

这是因为一旦您开始从输入流中读取(在服务器端),您的代码将被阻止(直到客户端将关闭其自己的输出流)。要调查这种情况,请运行您的服务器,然后使用telnet连接到它,如下所示:

telnet 127.0.0.1 4444

在每个行后按Enter键键入多行。并且看到您的服务器将为每个字符串打印-->,并且在关闭telnet会话之前,服务器不会继续进行。

因此您的客户应该将一些数据发送到服务器。然后它应该关闭输出流并重新连接到服务器。同时Server应该等待新连接。然后客户端可以从服务器下载文件。

更一般地说,您可以在单独的线程中读取套接字,因此您的主代码不会挂起。

代码(我也稍微更改了文件名):

服务器

    ServerSocket serverSocket = new ServerSocket(4444);
    Socket socket = serverSocket.accept();

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream("serverlog.txt"));


    int count;
    byte[] bytes = new byte[8192];

    while ((count = is.read(bytes)) > 0) {
        bos.write(bytes, 0, count);
        System.out.println("-->" + count);
    }

    System.out.println("DONE DOWNLOADING");
    bos.close();
    socket.close();


    /* SEND */
    socket = serverSocket.accept();
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("server.txt")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count2;
    byte[] bytes2 = new byte[8192];

    while ((count2 = fileInput.read(bytes2)) > 0) {
        out.write(bytes2, 0, count2);
        System.out.println("--> " + count2);
    }
    out.close();

    /* CLOSE */
    socket.close();
    serverSocket.close();

客户端

    Socket socket = new Socket("127.0.0.1", 4444);

    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("client.txt")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
    int count;        
    byte[] bytes = new byte[8192];
    while ((count = fileInput.read(bytes)) > 0) {
    out.write(bytes, 0, count);
        System.out.println("-->" + count);
    }
    System.out.println("DONE UPLOADING");
    out.close();
    socket.close();

    /* GET */
    socket = new Socket("127.0.0.1", 4444);
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("clientlog.txt"));

    int count2; 
    byte[] bytes2 = new byte[8192];

    while ((count2 = is.read(bytes2)) > 0) {
        bos.write(bytes2, 0, count2);
        System.out.println("-->" + count2);
    }
    bos.close();

    /* CLOSE */
    socket.close();
}