2016-03-02 22 views
0

我正在開發一個Android應用程序和一個Java桌面應用程序。 Android應用程序向java桌面發送收到的短信,桌面應用程序提供用於回答這些短信的界面。EUTException在readUTF後隨機

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。一切工作正常,直到某一點,我不知道爲什麼......有人?

+0

嘗試在字符串/文件末尾發送-1我認爲讀者不知道什麼時候停止閱讀,所以它會拋出異常 –

+0

你的意思是,當我在android應用程序中使用writeUTF?像myString +「 - 1」? – IronRabbit

+0

@ItzikSamara這不會完成任何事情,特別是它不會看起來像流的結束。讀者會拋出一個異常,因爲它*知道何時停止閱讀。 – EJP

回答

0

你得到這不是'隨機',而是當對方已關閉連接。

+0

同行?對不起,這可能是我的英語,但我不明白你的意思... – IronRabbit

+0

在連接的另一端的傢伙。 – EJP

+0

怎麼可能......?這兩個應用程序保持活着。 – IronRabbit