0
try {
            socket = new Socket("localhost", 9999);

            while(true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                byte[] buf = new byte[1024];
                FileOutputStream fout = new FileOutputStream("c:/hyebin/hyebin/excercise.jpg");

                while((ois.read(buf, 0, buf.length))!=-1){
                    fout.write(buf, 0, buf.length);
                }
                System.out.println("파일 수신 및 저장 성공");

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

java.io.EOFException
at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2890)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3385)
at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:942)
at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:385)
at comm/comm.client.main(client.java:14)

This is my client code. The first code runs normally. However, such an error occurs from the second time it is saved. I don't know where the error occurs.

hyebin
  • 5
  • 3

1 Answers1

2

This code is all sorts of problematic.

  1. The while(true) is just bizarre, and causes the exception you see.
  2. ObjectInputStream is used, but for absolutely no reason. plain jane inputstreams have the read() function. Don't wrap your socket in the ObjectInputStream.
  3. ois.read does not read the full buffer. It reads at least 1 byte, and probably more, possibly the entire buffer, but it doesn't have to. You ignore how many bytes it did read, and then write the full buffer to the file, which means that if for example the network packet size is smaller than the file size, your code breaks. you need to save the value returned by the read call, if it is -1, stop, if it is not, write that many bytes (provide that value instead of buf.length as third arg to the write method
  4. Your exception handling is deplorable. On failure, this code prints and continues.
  5. The error is in line 14 - see the client.java:14 bit in your stack trace? That's useful information.
  6. Resources must be guarded: You MUST close resources, and you must do so even if your code exits via exception or otherwise. There is a construct for this, called the ARM construct (automatic resource management), also called try-with.
  7. inputstreams have a method these days to ask them to just dump themselves into a file.

Combining it all:

private static final String OUT_FILE = "c:/hyebin/hyebin.exercise.jpg";
public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost", 9999);
    try (
        InputStream in = socket.getInputStream();
        FileOutputStream out = new FileOutputStream(OUT_FILE)) {

        in.transferTo(out);
    }
    System.out.println("파일 수신 및 저장 성공");
}

easy, isn't it? It's generally a good idea to look at the javadoc of stuff you're using to figure out how to use it, and to find any useful shortcuts, such as the transferTo method.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72