Problems with catching dataInputStream.readUTF();

3.1k views Asked by At

I'm making a Java program to make my computer a server to communicate with my smartphone over WiFi. Therefore I use the Socket class, as can be seen in the code below (based on Android-er):

package main;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerCommunication {

    @SuppressWarnings("resource")
    public static void main(String[] args){
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
        String message = null;

        try {
            int portNumber = 8888;
            serverSocket = new ServerSocket(portNumber);
            System.out.println("Listening :" + Integer.toString(portNumber));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while(true){
            try {
                clientSocket = serverSocket.accept();
                dataInputStream = new DataInputStream(clientSocket.getInputStream());
                dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
                System.out.println("ip: " + clientSocket.getInetAddress());
                System.out.println("message: " + dataInputStream.readUTF());
                dataOutputStream.writeUTF("test");

                message = dataInputStream.readUTF(); // <--- PROBLEM LINE

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                if( clientSocket!= null){
                    try {
                        clientSocket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if( dataInputStream!= null){
                    try {
                        dataInputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if( dataOutputStream!= null){
                    try {
                        dataOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

Everything works perfectly when the problem line (indicated with <---) is not present in the file. The message I receive is properly printed in the console. But form the moment I want to store this message in a String, I get a java.io.EOFException...

Can anyone tell me why I can print a message, but not store it as a string?

Thanks in advance!

3

There are 3 answers

1
Sathish On BEST ANSWER

The exception java.io.EOFException says that all the data in the stream is read, looks like you are trying to consume the data twice, one in the following statement,

System.out.println("message: " + dataInputStream.readUTF());

and the next one in,

dataInputStream.readUTF()

Either write more data from the writing side (client side) or consume once. Hope it helps.

1
gne On

Once you call

  dataInputStream.readUTF();

it pops the string and you print that one. Then in the second call since there are no more data in outputstream the End Of File exception occurs.

You may try storing the popped string to a variable and then printing it:

String  message = dataInputStream.readUTF();
System.out.println("message: " + message);
0
ask4solutions On

Remove your problem line

message = dataInputStream.readUTF(); // <--- PROBLEM LINE