在套接字中传输文件时出现异常

时间:2013-12-26 08:54:52

标签: java sockets exception

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

这是代码:

服务器端:

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 

2 个答案:

答案 0 :(得分:0)

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

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

通常你有两个频道,一个为整个交易开放,一个为每个文件打开。

答案 1 :(得分:0)

&#39;套接字关闭&#39;意味着你关闭了套接字然后继续使用它。

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