0

I am developping an Android app and a java desktop app. The Android app send to the java desktop the sms received, and the desktop app provide an interface for answering to these sms.

The android app is the server, the desktop app connects to it through a socket.

Here is the code of the server (android app side)

public class Server extends AsyncTask<Void, Void, Void> {


    public void stopServ(){
        this.run=false;
    }

    public void newSMSReceived(String sms, String phone){
        //SEND THE NEW SMS TO THE DESKTOP APP
        try {
            outputStream.writeUTF(new String(sms.getBytes(),"ISO-8859-1"));
            outputStream.flush();
            outputStream.writeUTF(new String(phone.getBytes(),"ISO-8859-1"));
            outputStream.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        ServerSocket ss = null;

        try {
            ss = new ServerSocket(TCP_SERVER_PORT);

            Socket s = ss.accept();
            System.out.println("connection received !");

            inputStream = new ObjectInputStream(s.getInputStream());
            outputStream = new ObjectOutputStream(s.getOutputStream());

            outputStream.writeObject(contacts);
            outputStream.flush();

            while(true){
                //READ THE MESSAGE SENDED FROM THE DESKTOP APP
                message=inputStream.readUTF();
                phone=inputStream.readUTF();

                smsManager.sendTextMessage(phone.replaceFirst("0", "\\+33"), null, message, null, null);
            }

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

}

The desktop app side :

public class Main extends Application {


    public Main(){
        try {
            PropertiesRetriever prop = new PropertiesRetriever();
            socket = new Socket(prop.getIp(), 5657);

            outputStream = new ObjectOutputStream(socket.getOutputStream());
            inputStream = new ObjectInputStream(socket.getInputStream());

            Thread listener = new Thread(new Runnable() {
                public void run() {
                    while(true){
                        String message,phone;
                        Contact contact;
                        try {
                            //RECEIVED THE MESSAGE FROM THE ANDROID APP
                            message=inputStream.readUTF();<--- EOFException
                            phone=inputStream.readUTF();

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } 

                    }
                }
            });

            listener.start();


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(Contact contact, Message message){
        try {
            //SEND THE MESSAGE TO THE ANDROID APP
            outputStream.writeUTF(message.getTextUTF());
            outputStream.flush();
            outputStream.writeUTF(contact.getPhoneUTF());
            outputStream.flush();
            System.out.println(message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**...**/
}

The details of the method "getxxUTF":

String rtr=null;

        try {
            rtr = new String(text.getBytes(),"ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rtr;

EOFException :

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
    at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:2818)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2874)
    at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1073)

Thing is, at a point, I get an EOFException on the readUTF mentioned above. Everything works fine until a certain point, and I have no clue why... Someone ?

IronRabbit
  • 185
  • 1
  • 2
  • 13
  • trying sending -1 at the end of the String/File i think the reader dont know when to stop reading so it throws exception – Itzik Samara Mar 02 '16 at 19:37
  • You mean, when i use writeUTF in the android app ? like myString+"-1" ? – IronRabbit Mar 02 '16 at 19:42
  • @ItzikSamara That won't accomplish anything, and specifically it won't look like an end of stream. The reader throws an exception because it *does* know when to stop reading. – user207421 Mar 04 '16 at 07:51

1 Answers1

0

You get this not 'randomly' but when the peer has closed the connection.

user207421
  • 305,947
  • 44
  • 307
  • 483