Search code examples
javaandroidsocketsdatainputstream

Problems with catching dataInputStream.readUTF();


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!


Solution

  • 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.