title: async Task : connect to tcp server and send a string ,close connection
body: <p>I am a beginner in android development, </p>
  
  <p>I have to do the following when a button is clicked using a Async Task.</p>
  
  <ul>
  <li>connect to a specific TCP server using ip and Port and Check if its connected? 
  on failure show a toast message</li>
  <li>on success send a string to the tcp server</li>
  <li>close the connection.</li>
  </ul>
  
  <p>I had used the code below for connecting </p>
  
  <pre><code>  try
    {
       s= new Socket("192.168.43.205",20108);
       out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
       BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  
    }
    catch (UnknownHostException e) {
       tv.setText(e.toString());
       Log.v("Tcp", e.toString());
     } 
     catch (IOException e) {
        tv.setText(e.toString());
        Log.v("Tcp",e.toString());
     }
     catch (Exception e) {
        tv.setText(e.toString());
  
     }
  </code></pre>
  
  <p>but this usually hangs when the server isn't available. Is there a fix for this?</p>