Java -下载后开始上传



我的程序只能做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();

如果我注释掉服务器的Get和客户端的Send:程序工作。服务器可以下载文件

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

不注释掉任何东西:只有服务器可以下载文件。有什么问题吗?

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

telnet 127.0.0.1 4444

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

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

更一般地说,你可以在单独的线程中读取socket,这样你的主代码就不会挂起。

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

服务器

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

相关内容

  • 没有找到相关文章

最新更新