1

I have created small CLI client-server application. Once the server is loaded the client can connect to it and send commands to the server.

The first command is to get list of files that server is loaded with.

Once the socket connection is established. I request user to enter a command.

ClientApp.java

Socket client = new Socket(serverName, serverPort); 

Console c = System.console();
if (c == null) {
    System.err.println("No console!");
    System.exit(1);
}

String command = c.readLine("Enter a command: ");

OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF(command);  

Then the server capture user's command and send appropriate replies.

SeverApp.java -

Socket server = serverSocket.accept();
DataInputStream in = new DataInputStream(server.getInputStream());

switch (in.readUTF()){
    case "list":
        for (String fileName : files) {
            out.writeUTF(fileName);
        }
        out.flush();

}
server.close();

Next the client retrieve server's response -

ClientApp.java

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
String value;
while((value = in.readUTF()) != null) {
    System.out.println(value);
}

client.close();

files is a ArrayList which i keep list of files loaded to the sever. When client sends list command to the sever, i need to send array of strings (list of file names) back. Similarly app will have more commands.

Now when i do such request i get list of files and trows java.io.EOFException from while((value = in.readUTF()) != null) {

How to fix this ?


EDIT (SOLUTION) ---

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

try {
    while (true) {
        System.out.println(in.readUTF());
    }
    } catch (EOFException e) {
}
ChamingaD
  • 2,908
  • 8
  • 35
  • 58
  • possible duplicate of [Java FileInputStream ObjectInputStream reaches end of file EOF](http://stackoverflow.com/questions/2626163/java-fileinputstream-objectinputstream-reaches-end-of-file-eof) – donfuxx Apr 14 '14 at 16:19

3 Answers3

3

The method readUTF will never return null. Instead, you should do:

while(in.available()>0) {
    String value = in.readUTF();

Looking at javadocs, an EOFException is thrown if this input stream reaches the end before reading all the bytes.

JamesB
  • 7,774
  • 2
  • 22
  • 21
1

Use FilterInputStream.available().

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
String value;
while(in.available() > 0 && (value = in.readUTF()) != null) {
    System.out.println(value);
}

...
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Braj i have anther question. In my code i loop through array and send data one by one to the client. Is it possible to send it once as string[] ? – ChamingaD Apr 15 '14 at 04:03
  • Sorry the solution seems not working. its giving `java.net.SocketException: Software caused connection abort: socket write error` from `out.writeUTF(fileName);` – ChamingaD Apr 15 '14 at 04:16
  • Its working file for me. Please have a look at my updated post. – Braj Apr 15 '14 at 08:26
  • Find out the difference and tell me what were you doing wrong? – Braj Apr 15 '14 at 08:27
  • The problem is in server side code where you have closed the Server Socket `serverSocket.close();` in `while` loop and trying to accept another client socket on same Server Socket. To solve this issue move `serverSocket.close();` outside `while` loop. – Braj Apr 15 '14 at 12:33
  • I haven't used any `while` loop in my server side code. server is stopped after committed data to single client. but you can do it for multiple clients also. – Braj Apr 15 '14 at 12:39
  • If still there is any issue then try with `BufferedReader` in place of `Scanner`. I have updated it in my post for client side code. – Braj Apr 15 '14 at 12:47
  • If still not working the remove `serverSocket.setSoTimeout(100000);` from your server side code. – Braj Apr 15 '14 at 22:21
  • I think i have found a solution. Thanks for your support. Updated my question. – ChamingaD Apr 16 '14 at 06:37
0

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

try {
    while (true) {
        System.out.println(in.readUTF());
    }
    } catch (EOFException e) {
}
ChamingaD
  • 2,908
  • 8
  • 35
  • 58