0

I have code TCP Server which sending a file and I am trying to save to file this bytes from client but the client freeze on read. Program dont give any exception and it is hard to debug.

Server Code:

private void byteSend(Result result) {
        try {
            BufferedOutputStream outToClient = new BufferedOutputStream(clientSocket.getOutputStream());

            File file = new File(Service.getInstance().getServer().getShareDirectory() + "/" + result.getData().toString().replace("__", " "));
            byte[] byteArray = new byte[(int) file.length()];

            FileInputStream fis;

            try {
                fis = new FileInputStream(file);

                BufferedInputStream bis = new BufferedInputStream(fis);
                bis.read(byteArray, 0, byteArray.length);
                outToClient.write(byteArray, 0, byteArray.length);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }

            outToClient.flush();
            outToClient.close();

            clientSocket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

Client code:

// create file input stream
                    InputStream is = clientSocket.getInputStream();

                    // create new data input stream
                    DataInputStream dis = new DataInputStream(is);

                    // available stream to be read
                    int length = dis.available();

                    // create buffer
                    byte[] buf = new byte[length];

                    // read the full data into the buffer
                    dis.readFully(buf);

                    // for each byte in the buffer
                    for (byte b:buf)
                    {
                        // convert byte to char
                        char c = (char)b;

                        // prints character
                        System.out.print(c);
                    }
                    break;

How to resolve this problem? Can you give me some tips?

user3537765
  • 301
  • 1
  • 4
  • 10

0 Answers0