0

i want to use the same socket address for multiple calls for this task.

i tried to get the address from different class but didn't work .

please help !!!

my call from different class is:

new ConnectToServer().execute(ip, "2000", "br1");

my code for task is:

public class ConnectToServer extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... parms) {
        // TODO Auto-generated method stub
        try {

            Socket s = new Socket(parms[0], Integer.parseInt(parms[1]));
            // ******

            OutputStream os = s.getOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(os);

            if (parms[2] == "br1") {
                out.write("rr1".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "br2") {

                out.write("rr2".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "br3") {
                out.write("rr3".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "br4") {
                out.write("rr4".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "bb1") {
                out.write("bb1".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "bb2") {
                out.write("bb2".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "bb3") {
                out.write("bb3".getBytes());
                out.write('\n');
                out.flush();
            } else if (parms[2] == "bb4") {
                out.write("bb4".getBytes());
                out.write('\n');
                out.flush();
            }

            // s.shutdownOutput();

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
saddam
  • 11
  • 2

1 Answers1

0

You can't compare java Strings with String == String. You need to use String.equals(String) instead.

Try this code:

        if (parms[2].equals("br1")) {
            out.write("rr1".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("br2")) {

            out.write("rr2".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("br3")) {
            out.write("rr3".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("br4")) {
            out.write("rr4".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("bb1")) {
            out.write("bb1".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("bb2")) {
            out.write("bb2".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("bb3")) {
            out.write("bb3".getBytes());
            out.write('\n');
            out.flush();
        } else if (parms[2].equals("bb4")) {
            out.write("bb4".getBytes());
            out.write('\n');
            out.flush();
        }
Arielle
  • 1,241
  • 2
  • 11
  • 14
  • i talk about the socket not String. – saddam Nov 23 '13 at 11:39
  • Please give a stack trace or a more in-depth explanation of what's wrong. – Arielle Nov 23 '13 at 11:51
  • sorry i am beginner,,, when i call this class (new ConnectToServer().execute(ip, "2000", "br1");)....it is create new socket ,when i recall its create new socket and so on , i want to use the same address for all calls?? your correct code should solve Socket s=***THE CORRECT***new Socket(ip,port); – saddam Nov 24 '13 at 07:41