【问题标题】:EOFException after readUTF randomly随机读取UTF后的EOFException
【发布时间】:2016-03-02 19:31:47
【问题描述】:

我正在开发一个 Android 应用程序和一个 Java 桌面应用程序。 Android 应用将收到的短信发送到 java 桌面,桌面应用提供一个接口来回复这些短信。

android 应用程序是服务器,桌面应用程序通过套接字连接到它。

这里是服务端的代码(安卓应用端)

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;
    }

}

桌面应用端:

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();
        }
    }

    /**...**/
}

“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;

EOF异常:

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)

问题是,我在上面提到的 readUTF 上得到了 EOFException。一切正常,直到某一点,我不知道为什么......有人?

【问题讨论】:

  • 尝试在字符串/文件末尾发送 -1 我认为读者不知道何时停止阅读,因此会引发异常
  • 你的意思是,当我在 android 应用程序中使用 writeUTF 时?像 myString+"-1" 吗?
  • @ItzikSamara 这不会完成任何事情,特别是它看起来不像流的结束。阅读器抛出异常,因为它确实知道何时停止阅读。

标签: java android sockets objectinputstream eofexception


【解决方案1】:

您不是“随机”得到的,而是在对等方关闭连接时得到的。

【讨论】:

  • 同行?对不起,这可能是我的英语,但我不明白你的意思......
  • 连接另一端的人。
  • 怎么可能……?两个应用程序都保持活力。
  • 对等端已经关闭了他的连接端:他的套接字。这并不意味着他已经退出了。
  • 好的,有没有办法不停止套接字?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2014-05-16
  • 1970-01-01
  • 2020-11-29
  • 2011-03-04
相关资源
最近更新 更多