【问题标题】:Receiving multiple errors on the socket Java在套接字 Java 上收到多个错误
【发布时间】:2013-12-23 07:19:47
【问题描述】:

我正在尝试使用套接字创建文件传输系统。在我开始将字符串文件名从服务器发送到客户端以使文件具有相同名称之前,我的代码曾经正常工作。现在,每当我尝试发送文件时,它总是在客户端和服务器中给我不同的错误。

服务器端代码:

public void soc_server() throws IOException {

   long time = System.currentTimeMillis();

    long totalSent = 0;
    ServerSocket servsock = null;
    Socket sock = null;
    PrintWriter pw = null;
    FileInputStream fileInputStream = null;

    try {

        servsock = new ServerSocket(55000);
        sock = servsock.accept();
        System.out.println("Hello Server");
        Scanner sc = new Scanner(System.in);

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

        String s = sc.nextLine();

        sc.close();

        File file = new File(s);

        if (file.exists())

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

        OutputStream out = sock.getOutputStream();

        pw = new PrintWriter(sock.getOutputStream(), true);

        pw.print(s);

        fileInputStream = new FileInputStream(s);

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

        int bytesRead = 0;

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

        }

    } catch (Exception e) {

        System.out.println("exception " + e);

    } finally {
        sock.close();

        pw.close();

        servsock.close();

        fileInputStream.close();

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

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

    }

}

客户端代码:

public void soc_client() throws Exception {
    long time = System.currentTimeMillis();
    long totalRecieved = 0;
    Socket sock = null;
    InputStream in = null;
    BufferedReader br = null;
    FileOutputStream fileOutputStream = null;

    try {
        sock = new Socket("172.16.27.106", 55000);
        System.out.println("Hello Client");
        in = sock.getInputStream();
        br = new BufferedReader(new InputStreamReader(in));
        String fileName = br.readLine();
        File outputFile = new File(fileName + "");
        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");
        }

    } catch (Exception e) {
        System.out.println("Exception " + e);
    } finally {
        br.close(); // CLOSING BufferedReader
        fileOutputStream.close();
        sock.close();
        System.out.println("Recieved " + totalRecieved + " bytes in "
                + (System.currentTimeMillis() - time) + "ms.");
    }
}

例外:

客户端:

Exception java.io.FileNotFoundException: Invalid file path
Exception: java.lang.NullPointerException

Exception in thread "main" java.io.FileNotFoundException: Invalid file path 
          at java.io.FileOutputStream.<init>(Unknown Source) 
          at java.io.FileOutputStream.<init>(Unknown Source) 
          at Client.soc_client(Client.java:25) 
          at Index.main(Index.java:24)

服务器端:

Exception java.net.SocketException: Connection reset
Exception: java.util.NoSuchElementException
Exception java.net.SocketException: Broken pipe

Exception in thread "main" java.net.SocketException: Connection reset 
          at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
          at java.net.SocketOutputStream.write(SocketOutputStream.java:153) 
          at Server.soc_server(Server.java:59) 
          at Index.main(Index.java:21) 

我尝试发送的文件与我从中编译类的目录(桌面)相同。 谢谢你。

【问题讨论】:

  • 请发布堆栈跟踪并指出它们被抛出的行。
  • 我已经用服务器和客户端的堆栈跟踪编辑了主帖子。请看一看。谢谢

标签: java sockets exception file-transfer


【解决方案1】:

尝试在客户端的路径中直接给出文件名。

File outputFile = new File("yourfile.txt");

然后发送到服务器。

由于客户端出现异常FileNotFound,您正在关闭finaly 块处的流。

当您关闭客户端的流时,服务器端无法识别它正在读取的流,因此给出Connection reset 异常。

由于服务器端没有用于读取数据的流,因此您将收到 NoSuchElement 异常

编辑

另一件事是,您不会在写入客户端后刷新流,

pw.flush();pw.print(s)out.write() 之后也是如此

【讨论】:

  • 但是我想在客户端和服务器端都有相同的文件名。那我怎么能保证呢?谢谢
  • 然后打印filename,然后在客户端读取您从服务器获取的内容,并查看该文件名路径是否存在于客户端系统中。
  • 文件路径不存在。所以,我再次给了 outputFile.createNewFile() 命令。错误仍然存​​在。
  • 找不到那个文件的异常还会来吗?
  • 我的实际代码是 outputFile.mkdir(); outputFile.createNewFile();现在我有一个名为 null 的空文件夹,并且错误仍然存​​在。在服务器端,没有这样的元素异常,在客户端也没有异常。它出现在客户端,它没有收到任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多