title: android onclick buttons to write strings on Socket
body: <p>Hi I am building an app which requires the user to press buttons (total 8 buttons). These buttons used to send strings to the server onclick . The problem here I'm having is when i press any of the button after connecting to the server it sends the string as it should but the 2nd time nothing happens. I was suggested to use doInBackground() from AsyncTask, to run keep running the socket and write to it each time the buttons are pressed. But i am unable to do so. What should I do ? I don't know where is the problem. Here I'm putting my code.</p>
  
  <p>This is my Activity</p>
  
  <pre><code>public class Acontroller extends Activity {
  
  Button bForward;
  Button bBackward;
  Button bRight;
  Button bLeft;
  Button bSelect;
  Button bStart;
  Button bB;
  Button bA;
  Socket s;
  DataOutputStream os;
  String ip;
  
  // MyAppActivity ip = new MyAppActivity();
  MyThread start = new MyThread();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
  
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
  
      super.onCreate(savedInstanceState);
  
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
      setContentView(R.layout.nesskin);
  
      bForward = (Button) findViewById(R.id.bForward);
      bBackward = (Button) findViewById(R.id.bBackward);
      bRight = (Button) findViewById(R.id.bRight);
      bLeft = (Button) findViewById(R.id.bLeft);
      bSelect = (Button) findViewById(R.id.bSelect);
      bStart = (Button) findViewById(R.id.bStart);
      bA = (Button) findViewById(R.id.bA);
      bB = (Button) findViewById(R.id.bB);
  
      Bundle gotIP = getIntent().getExtras();
      ip = gotIP.getString("ipAddress");
  
  
  //  start.doInBackground(ip);
      //start.execute(ip);
      // sock.start();
  
  }
  
  protected void onStart() {
      // TODO Auto-generated method stub
      super.onStart();
  
      bForward.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
  
              start.execute(ip);
          }
      });
  </code></pre>
  
  <p>And from the thread.</p>
  
  <pre><code>public class MyThread extends AsyncTask &lt;String, Void, String&gt;{
  
  
  Socket s;
  DataOutputStream os;
  String ip;
  String cmd;
  
  
    @Override
    protected void onPreExecute() {
        Log.i("AsyncTask", "onPreExecute");
    }
  
  
  
  @Override
  protected String doInBackground(String... params) {
      int port = 2222;
  
  
      // TODO Auto-generated method stub
  
      if(s.isConnected()){
          try {
              os.writeUTF("forward");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }else{
  
  
  
      try {
      s= new Socket(ip, port);
      os = new DataOutputStream(s.getOutputStream());
      os.writeUTF("forward");
  
      }catch(Exception e){
  
          e.printStackTrace();
      }
  
  
      finally{
            if (s!= null){
             try {
              s.close();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
            }
  
            if (os != null){
             try {
              os.close();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
            }
      }
  
  
  }
  
  
      return null;
  }
  </code></pre>
  
  <p>My objective is as many times the user clicks the buttons the respective strings must be send to the server. Here is my server code I'm using PC as server.</p>
  
  <pre><code>public class Server
  
  
  public static void main(String[] args) throws AWTException {
      String msg_received = null;
      String fw = "forward";
      String bw = "backward";
      String l = "left";
      String r = "right";
      String se = "select";
      String st = "start";
      String a = "a";
      String b = "b";
      String fin = "finish";
  //  Boolean finish = (msg_received.equal(fin));
      Robot robot = new Robot();
  
      try {
          ServerSocket ss = new ServerSocket(2222);
          System.out.println("Server Started...");
  
          while (true) {
              Socket s = ss.accept();
  
              System.out.println("Connection Request Received");
  
              DataInputStream DIS = new DataInputStream(s.getInputStream());
              msg_received = DIS.readUTF();
              System.out.println(msg_received);
              // s.close();
              // ss.close();
  
              if (msg_received.equals(fw)) {
                  // tu yeh kerna
  
                  robot.keyPress(KeyEvent.VK_UP);
                  robot.keyRelease(KeyEvent.VK_UP);
  
              }
  </code></pre>
  
  <p>Please help me it really important to me. Thanks in advance.</p>