EOFException in readUTF

raja shekar picture raja shekar · Jul 31, 2013 · Viewed 11.6k times · Source

I am getting below EOFException while using readUTF() method, please let me know how could i overcome with this problem and also please suggest how readUTF() transfers socket information over the other networks

import java.io.*;
import java.net.*;

public class GreetingServer {
    public static void main(String args[]) {
        String servername =args[0];
        int port = Integer.parseInt(args[1]);

        try {
            System.out.println("Server Name "+ servername +"Port"+port);

            Socket client = new Socket(servername,port);
            System.out.println("Just connected to"+ client.getRemoteSocketAddress());
            OutputStream outs = client.getOutputStream();
            DataOutputStream dout = new DataOutputStream(outs);
            dout.writeUTF("Hello From"+client.getRemoteSocketAddress());
            InputStream in = client.getInputStream();
            DataInputStream din = new DataInputStream(in);
            System.out.println("Server Says"+ din.readUTF());
            client.close();
        }
        catch (EOFException f) {
            f.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

Answer

user207421 picture user207421 · Aug 9, 2013

You have reached the end of the stream. There is no more data to read.

Possibly your server isn't using writeUTF(), or you are out of sync with it. If the server is writing lines you should be using BufferedReader.readLine().