I am writing a file input/output program that will use the Java socket, and I am asking you this question because of the EOFException process in readUTF to the client

Asked 2 years ago, Updated 2 years ago, 74 views

A program where multiple clarifiers access and chat on a single server

When the server enters option, the menu pops up, and when you select File Transfer, you enter the file path, decide who to send it to, and then send it to the client, and the client receives the file.

When the client enters Y or y, it sends the string "Please send the File" to the server The server sends the "File Transfer" string and immediately starts transferring the file At the same time, the client starts receiving the file when it receives the string "Send the file."

The client receives all the files normally, but at the end, the client disconnects and leaves while processing the EOFException. The picture below is the client.

It's a code.

[AServer.java]

                if(c_s.elementAt(i).name.equals(name))
            {                           
                c_s.elementAt(i).dos.writeUTF ("Send file").;
                //Send Logic
                fis = new FileInputStream(filePath);
                bis = new BufferedInputStream(fis);
                int len = 0;
                int size = 4096;
                byte[] data = new byte[size];
                while ((len = bis.read(data, 0, 4096)) != -1) {
                    c_s.elementAt(i).dos.write(data, 0, len);
                    c_s.elementAt(i).dos.flush();
                }
                System.out.println ("File Transfer Operation Completed".");
                System.out.println ("Size of sent file: " + sendFile.length()));

                c_s.elementAt(i).dos.close();
                bis.close();
                fis.close();
                break;
            }
        }
    }

[AClient]

 else if(msgClient.equals("Send file")
            {       
                pln ("Start receiving transmissions!");
                fos = new FileOutputStream(f);
                bos = new BufferedOutputStream(fos);

                int len = 0;
                int size = 4096;
                byte[] data = new byte[size];

                while ((len = dis.read(data, 0, 4096)) != -1) {
                    bos.write(data, 0, len);
                    bos.flush();
                } 
                System.out.println ("File Received" completed.");
                System.out.println ("Incoming file size: " + f.length()));                   
                bos.close();
                fos.close();
                dis.close();
            }

java file-io socket

2022-09-22 21:58

3 Answers

First of all, I want to know which part of the clarification code is the part where the error occurs. If you look at the image file, there seems to be an error in readUnsignedShort. The client code you uploaded doesn't have a part that calls readUnsignedShort.

Is it right to receive all the files normally? If so, I don't think it will end because of the exception that the client has not processed if you cover the exception with try/catch.


2022-09-22 21:58

I don't know the full code, so if I can guess... The error was called dis.close() from the client, but I think I read it again from dis after that.

System.out.println ("File Received" completed.");
                System.out.println ("Incoming file size: " + f.length()));                   
                bos.close();
                fos.close();
                dis.close(); // close input stream
            }
            // It is assumed that an error occurred after reading something from the input stream.


2022-09-22 21:58

It is recommended that you send the file's capacity before transferring the file, and then the server sends the contents of the file. After receiving the capacity information, the client reads only the capacity and stores it.

If you don't send the capacity, the client side won't know how long it will be read, so it keeps trying to read the information.


2022-09-22 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.