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. 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.

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.

As no stream is there for reading data at server side, you are getting NoSuchElement exception

EDIT

Another thing is, you are not flushing the stream after writing to client,

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top