【问题标题】:exception while transferring files in socket在套接字中传输文件时出现异常
【发布时间】:2013-12-26 08:54:51
【问题描述】:

我在这里通过套接字处理客户端-服务器文件传输应用程序。在这里,我的问题是我的客户端抛出异常,说在服务器端仍然打开时套接字已关闭。它仍然提示要发送下一个文件名。我没有关闭套接字或其他任何东西。

代码如下:

服务器端:

public void soc_server() throws IOException {

    servsock = new ServerSocket(55000);

    sock = servsock.accept();
    pw = new PrintWriter(sock.getOutputStream(), true);
    System.out.println("Hello Server");

    Scanner sc = new Scanner(System.in);

    System.out.println("how many files to be sent: ");
    String temp = sc.nextLine();
    temp = temp.trim();
    int fileNumber = Integer.parseInt(temp);
    // sc.close();
    // sc = new Scanner(System.in);

    for (int x = 0; x < fileNumber; x++) {

        System.out.println("Please enter the file name or file path ");

        String s = sc.nextLine();

        File file = new File(s);

        if (file.exists())
            System.out.println("File found");
        else
            System.out.println("File not found");

        out = sock.getOutputStream();

        fileInputStream = new FileInputStream(s);

        byte[] buffer = new byte[100 * 1024];

        int bytesRead = 0;

        System.out.println("sending file no: " + x);

        while ((bytesRead = fileInputStream.read(buffer)) != -1) {

            if (bytesRead > 0) {

                out.write(buffer, 0, bytesRead);

                totalSent += bytesRead;
            }

        }

        System.out.println("sent " + (totalSent / 1024) + " KB "
                + ((System.currentTimeMillis() - time) / 1000) + " sec");

        out.flush();

        pw.print(s);

        pw.flush();
    }

    fileInputStream.close();

    out.close();

    pw.close();

    System.out.println("Sent " + (totalSent / 1024) + " kilobytes in "

    + ((System.currentTimeMillis() - time) / 1000) + " seconds");

    sock.close();

    servsock.close();

    sc.close();

}

客户端代码:

public void soc_client() throws Exception {
    sock = new Socket("172.16.27.106", 55000);
    System.out.println("Hello Client");

    while (sock.isConnected() == true) {

        in = sock.getInputStream();

        File outputFile = new File("outputFile");

        if (outputFile.exists())
            System.out.println("File found");
        else {
            System.out.println("File not found");

            outputFile.createNewFile();
        }

        fileOutputStream = new FileOutputStream(outputFile);

        byte[] buffer = new byte[100 * 1024];

        int bytesRead = 0;

        while ((bytesRead = in.read(buffer)) != -1) {

            fileOutputStream.write(buffer, 0, bytesRead);

            totalRecieved += bytesRead;

        }
        System.out
                .println("Recieved " + (totalRecieved / 1024)
                        + " kilobytes in "
                        + ((System.currentTimeMillis() - time) / 1000)
                        + " seconds");

        fileOutputStream.flush();

        fileOutputStream.close();

        in = sock.getInputStream();

        br = new BufferedReader(new InputStreamReader(in));

        String fileName = br.readLine();

        fileName = fileName + "";

        System.out.println(fileName);

        File file = new File(fileName);

        file.createNewFile();

        boolean success = outputFile.renameTo(file);

        if (!success) {

            System.out.println(" file renaming is unsuccessful ");
        } else {

            outputFile.delete();
        }

        in.close();

        br.close();

    }
    if (sock.isConnected() == true) {
        System.out.println(" connection stays ");
    } else {
        System.out.println(" connection terminated ");
    }

    System.out
            .println("Recieved "
                    + (totalRecieved
                            / (1024 * 1024)
                            + " MB in "
                            + ((System.currentTimeMillis() - time) / (1000 * 60)) + " minutes"));
}

客户端异常:

Press 1 to be server
Press 2 to be client
Press 3 to be emergency exit
2
Hello Client
File not found
Recieved 42 kilobytes in 12.148 secondsException in thread "main" 
null
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Unknown Source)
at Client.soc_client(Client.java:28)
at Index.main(Index.java:25)

服务器端提示:

asus@CM1855:~/Desktop$ java Index
Press 1 to be server
Press 2 to be client
Press 3 to be emergency exit
1
Hello Server
how many files to be sent: 
2
Please enter the file name or file path 
Bill of cloud draft.docx
File found
sending file no: 0
sent 36 KB 20 sec
Please enter the file name or file path 

【问题讨论】:

  • 我怀疑你可以像你那样在同一个套接字上多次调用getInputStream
  • @Heuster 我建议您尝试一下,而不是在这里发布您的猜测。
  • @EJP 哈哈谢谢你的提示。下次我会三思而后行。有像你这样的人使这个网站保持完美,这很好。我希望我能像你一样多一点。
  • @Heuster “更像我”与此无关。你只需要接受你在科学学科中,证据和实验规则适用。

标签: java sockets exception


【解决方案1】:

“套接字已关闭”表示您关闭了套接字,然后继续使用它。

关闭一个套接字的输入流或输出流会关闭另一个流和套接字。

【讨论】:

    【解决方案2】:

    您多次请求 Out- 和 Inputstream。 上次我使用套接字时,你只能得到一个。连续调用将返回相同的流。关闭它会关闭套接字

    您需要一个协议来识别新文件何时开始和结束,或者为每个文件打开和关闭一个新连接。

    通常你有两个通道,一个为整个事务打开,一个为每个文件打开。

    【讨论】:

    • 您上次使用套接字时得出了一个完全错误的结论。
    • 感谢@EJP 的建设性反馈。我最后一次使用它们实际上是几年前,所以事情可能已经改变,但阅读你自己的回应似乎仍然是真的,你不能在一个套接字上打开和关闭多个流。我的措辞不准确,所以我会加强:-)
    • 没有任何改变。正如您现在同意的那样,在 Socket 上多次调用 getInputStream() 或 getOutputStream() 总是返回相同的对象,因此不可能引起与 GC 相关的问题,正如您在原始答案中所说的那样,您现在已经更改了,并且从来都不是真的。你不能同时拥有它。
    猜你喜欢
    • 2018-05-18
    • 2011-09-03
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2014-08-02
    相关资源
    最近更新 更多