简体   繁体   English

在套接字Java上收到多个错误

[英]Receiving multiple errors on the socket Java

I am trying to create File Transfer system by using socket. 我正在尝试使用套接字创建文件传输系统。 My code used to work properly before I started sending a String fileName from server to Client to have the files in same name. 在我开始从服务器向客户端发送String fileName以使文件具有相同名称之前,我的代码曾经能够正常工作。 Now, whenever I try to send file, it keeps giving me different error in client and server. 现在,每当我尝试发送文件时,它总是在客户端和服务器中给我不同的错误。

Server side code: 服务器端代码:

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

    }

}

Client Side code: 客户端代码:

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

Exceptions: 例外情况:

Client Side: 客户端:

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)

Server Side: 服务器端:

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) 

The file I am trying to send is the same directory (Desktop) from which I am compiling the class. 我试图发送的文件与我在其中编译类的目录(桌面)相同。 Thank you. 谢谢。

Try to give file name directly in the path at client side. 尝试直接在客户端路径中提供文件名。

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

and then send it to server. 然后将其发送到服务器。

Because of exception FileNotFound at client side , you are closing the the stream at finaly block. 由于客户端FileNotFound ,您将在finaly块中关闭流。

As you are closing the stream of client side, the server side does not recognize the stream from which it is reading hence giving Connection reset exception. 当您关闭客户端流时,服务器端无法识别正在从中读取的流,因此Connection reset异常。

As no stream is there for reading data at server side, you are getting NoSuchElement exception 由于没有流可以在服务器端读取数据,因此您会收到NoSuchElement异常

EDIT 编辑

Another thing is, you are not flushing the stream after writing to client, 另一件事是,您不会在写入客户端后刷新流,

So do pw.flush(); pw.flush(); after pw.print(s) and out.write() pw.print(s)out.write()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM