-1

I use bellow codes for send file file master to client but when I use mulltiple time this code for different file program give "SOCKET CLOSED" error because of I close socket using " out.close(); " and "in.close();" I can't solve these problem Do you have any suggestion. Also I try to don't close socket but it is not worked next files is not send this time

private void SendFiletoClient(Socket Socket, String fileName) {
        try {
            File file = new File(MasterPath + "/" + fileName);// Get the size of the file
            long length = file.length();
            byte[] bytes = new byte[16 * 1024];
            InputStream in = new FileInputStream(file);
            OutputStream out = Socket.getOutputStream();

            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }

            out.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

/////

 private void addFileToClient(Socket Socket, String fileName) {

        File file = new File(MasterPath  + fileName);
        try {
            file.createNewFile();

            InputStream in = Socket.getInputStream();
            OutputStream out = new FileOutputStream(MasterPath  + fileName);

            byte[] bytes = new byte[16 * 1024];
            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
stigmata2
  • 73
  • 8

1 Answers1

0

Its because you are closing the socket

  out.close();
  in.close();

Either dont close socket and keep your connection alive, or open-close everytime you want to send something trough it.

In your case, close output(file) and keep alive input(socket). The question stands, what happen on the other side?

Antoniossss
  • 31,590
  • 6
  • 57
  • 99