随机读取readUTF后的EOFException

时间:2016-03-02 19:31:47

标签: java android sockets objectinputstream eofexception

我正在开发Android应用和Java桌面应用。 Android应用程序将收到的短信发送到java桌面,桌面应用程序提供了回答这些短信的界面。

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

这是服务器的代码(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;
    }

}

桌面应用程序端:

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

    /**...**/
}

方法的详细信息&#34; getxxUTF&#34;:

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)

事实上,我在上面提到的readUTF上得到了一个EOFException。一切都很好,直到某一点,我不知道为什么......有人?

1 个答案:

答案 0 :(得分:0)

你得到的不是'随机',而是当对等关闭连接时。