尝试读取输入流时程序停止工作

时间:2014-02-04 19:43:32

标签: java android multithreading sockets datainputstream

我有一个Java服务器和一个(或多个)Android客户端。现在我希望他们只用字符串进行沟通。当我从android编写时,我可以在Java Server中获取数据,但是当我尝试从服务器获得答案时,Android应用程序停止工作。代码报告如下:

Java Server

public class Server {

  private static int port=12346, maxConnections=0;
  // Listen for incoming connections and handle them
  public static void main(String[] args) {
    int i=0;
    try{
      ServerSocket listener = new ServerSocket(port);
      Socket server;
      while((i++ < maxConnections) || (maxConnections == 0)){
        doComms connection;
        server = listener.accept();
        String end = server.getInetAddress().toString();
        System.out.println("\n"+end+"\n");
        doComms conn_c= new doComms(server);
        Thread t = new Thread(conn_c);
        t.start();
      }
    } catch (IOException ioe) {
      System.out.println("IOException on socket listen: " + ioe);
      ioe.printStackTrace();
    }
  }

}

class doComms implements Runnable {
    private Socket server;
    private String line,input;

    public doComms(Socket server) {
      this.server=server;
    }

    @SuppressWarnings("deprecation")
    public void run () {

      input="";


      try {
        // Get input from the client
        DataInputStream in = new DataInputStream (server.getInputStream());
        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(server.getOutputStream())),
                         true);

        while((line = in.readLine()) != null && !line.equals(".")) {

            input=input + line;
        }

        JOptionPane.showMessageDialog(null, input);
        out.println("Enviado");

        server.close();
      } catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }
    }

和Android客户端的代码(每次在onClick方法中按下按钮时调用它):

public String enviaMensagem(){
        String resposta="";
        new Thread(new ClientThread()).start();

        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;


        try {
              socket = new Socket(ip, port);
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              dataOutputStream.writeUTF(input.getText().toString());
              resposta = dataInputStream.readUTF();
             } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             finally{
              if (socket != null){
               try {
                socket.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();
               }
              }

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


        return resposta;
    }

1 个答案:

答案 0 :(得分:1)

您使用的是readUTF(), writeUTF(), readLine(),等未分类的混合物。它们并非全部可互操作。其中一个安顿下来。如果您使用writeUTF(),则必须在另一端使用readUTF()。如果您使用readLine(),则必须在另一端写行,并使用\r\n\n等行终止符。