-1

I have a Java Server and one(or more) Android Clients. For now I want them to communicate simply with strings. When i write from android I can get the data in Java Server, but when I try to get the answer from server the Android application stop working. The codes is reported below:

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

And Android client's code (it's called every time a button is pressed inside onClick method):

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;
    }
eddie
  • 1,252
  • 3
  • 15
  • 20
  • Please post a stack trace – Philio Feb 04 '14 at 19:59
  • A quick look at the code, I can see that, maybe, the server waits for the final "." (!line.equals(".")). And the client waits for the server to respond which never happens. Are you sure you send the "." string to the server? – ja_mesa Feb 04 '14 at 20:12

1 Answers1

1

You are using an unsorted mixture of readUTF(), writeUTF(), readLine(), etc. They're not all interoperable. Settle on one of them. If you use writeUTF() you must use readUTF() at the other end. If you use readLine() you must write lines at the other end, with a line terminator such as \r\n or \n.

user207421
  • 305,947
  • 44
  • 307
  • 483