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



我有一个Java服务器和一个(或更多)Android客户端。目前,我希望他们与字符串进行交流。当我从Android写入时,我可以在Java服务器中获取数据,但是当我尝试从服务器获得答案时,Android应用程序停止工作。代码如下:

Java服务器

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

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

相关内容

  • 没有找到相关文章

最新更新