TCP android client does not work

0

Good afternoon,

I'm creating an application in android studio to send a string from smartphone to pc.

My application has the following code:

public void Send_Command(View v) {

  testClass();

  Toast.makeText(getApplicationContext(), "Comando Enviado!", Toast.LENGTH_LONG).show();

}

public static void testClass() {
  Thread cThread = new Thread(new ClientThread());
  cThread.start();
}

public static class ClientThread implements Runnable {

  String results = "";

  public void run() {
    try {
      InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
      System.out.println("C: Connecting...");
      while (true) {
        results = "";
        try {
          Socket socket = new Socket("192.168.1.77", 8888);
          BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
          out.write("Test");
          out.flush();
          String inMsg = "";
          boolean b = false;
          while (!b) {
            inMsg = in .readLine();
            if (inMsg != "")
              b = true;
          }
          socket.close();
          System.out.println("C: Closed.");
        } catch (Exception e) {
          System.out.println("S: Error" + e.toString());
        }
      }
    } catch (Exception e) {
      System.out.println("C: Error" + e);
    }
  }
}

I put the Send_Message in the onClick of a button and when I run the application with an open C # server on the pc nothing happens, however if I run the same android studio code in eclipse with the same server in open C # the connection is established and the string sent.

I'm new to programming for android, so it's possible that I'm making some basic mistakes, however I do not know where.

Does anyone know where the error might be?

Thank you

    
asked by anonymous 03.10.2018 / 20:05

0 answers